82

インライン ビュー テンプレートをロードしたかったのです。

テンプレートを type の script タグでラップしtext/ng-template、id を に設定しましたtemp1.html。そして、ここに私のモジュール構成がどのように見えるかがあります

learningApp.config(function ($routeProvider) {
    $routeProvider
        .when("/first",{ controller: "SimpleController", templateUrl: "temp1.html"})
        .when("/second", {controller: "SimpleController", templateUrl: "temp2.html"})
        .otherwise({redirectTo : "/first"});
});

GET http://localhost:41685/temp1.html 404 (Not Found)コンソール ウィンドウで、その名前のファイルを探していることを意味します。

私の質問は: インライン テンプレートを使用するようにルートを構成するにはどうすればよいですか?

更新: サーバーでレンダリングされた DOM は次のようになります

<!DOCTYPE html>
<html>
<head>
    <script src="/Scripts/angular.js"></script>
    <link href="/Content/bootstrap.css" rel="stylesheet"/>
</head>
<body>
    <div class="container">       
    <h2>Getting Started with Angular</h2>
    <div class="row">
        <div class="panel" ng-app="LearningApp">
            <div ng-view></div>
        </div>
    </div>

<script type="text/ng-template" id="temp1.html">
    <div class="view">
        <h2>First View</h2>
        <p>
            Search:<input type="text" ng-model="filterText" />
        </p>
        <ul class="nav nav-pills">
            <li ng-repeat="cust in customers | orderBy:'name' | filter: filterText "><a href="#">{{cust.name}} - {{cust.school}}</a></li>
        </ul>
    </div>
</script>

<script type="text/ng-template" id="temp2.html">
    <div class="view">
        <h2>Second View</h2>
        <p>
           Search:<input type="text" ng-model="filterText" />
        </p>
        <ul class="nav nav-pills">
            <li ng-repeat="cust in customers | orderBy:'name' | filter: filterText "><a href= "#">{{cust.name}} - {{cust.school}}</a></li>
        </ul>
    </div>
</script>
    </div>
    <script src="/Scripts/jquery-1.9.1.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
    <script src="/Scripts/app/LearningApp.js"></script>
 </body>
</html>
4

2 に答える 2

111

オーディ、あなたは正しい軌道に乗っていました。唯一の問題は、タグがディレクティブが使用されている DOM 要素の外側にあることでした。ng-app要素に移動すると、<body ng-app="LearningApp">インライン テンプレートが機能するはずです。

この質問も関連しているかもしれません: Is there a way to make AngularJS load partials in the first and not at when needed?

于 2013-04-20T21:00:13.567 に答える
33

script-Element の id-Attribute を使用して、テンプレートの名前を設定してみてください。

<script type="text/ng-template" id="temp1.html">
   ... some template stuff
</script>
于 2013-04-20T20:22:22.937 に答える