我想加载一个内联视图模板。
我将模板包装在类型为的脚本标签中text/ng-template,并将ID设置为temp1.html。这是我的模块配置
text/ng-template
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)在控制台窗口中告诉我,这意味着它正在寻找该名称的文件。
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>
Ody,您的方向正确,唯一的问题是标记位于使用指令的DOM元素 之外ng-app。如果将其移至<body ng- app="LearningApp">元素,则内联模板应该可以使用。
ng-app
<body ng- app="LearningApp">