我正在尝试包含要包含在ng-view
中的多个部分模板,但routeProvider
指令总是尝试从服务器(而不是嵌入式ng-template
)获取文件
我在HTML中定义了以下内容:
<script type="text/ng-template" id="groupList.html">
<!-- contents -->
</script>
<script type="text/ng-template" id="participantList.html">
<!-- contents -->
</script>
<script src="js/app.js"></script> <!-- AngularJS app file -->
在我的app.js
文件中,我有以下内容:
var myApp = angular.module('myApp', ['ngResource', '$strap.directives']);
// Update interpolateProvider for Django server
myApp.config(function ($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
});
myApp.config(function ($routeProvider)
{
$routeProvider.when('/', {
templateUrl: '../static/views/home.html'
});
$routeProvider.when('/group', {
controller: 'GroupCheckInCtrl',
templateUrl: 'groupList.html'
});
$routeProvider.when('/group/:groupId', {
controller: 'GroupCheckInCtrl',
templateUrl: 'participantList.html'
});
$routeProvider.otherwise({
redirectTo: '/'
});
});
当我加载页面时,我的浏览器控制台出错:
GET http://127.0.0.1:8000/visitorLog/group/groupList.html 404 (NOT FOUND)
我页面的AngularJS应用程序部分如下所示:
<div ng-app="myApp" ng-controller="GroupCheckInCtrl">
<!-- other content -->
<ng-view>Loading...</ng-view>
</div>
我错过了哪些不允许AngularJS加载我的ng-template
脚本指令?
答案 0 :(得分:13)
确保内联脚本标记是具有ng-app =“myApp”属性的元素的子元素。如果脚本标记在此元素之外,则它将不起作用。解决此问题的最简单方法是将“ng-app ='myApp'”添加到标记中。这是我的意思的一个例子:
<body ng-app="plunker" ng-controller="MainCtrl">
<script type="text/ng-template" id="groupList.html">
Group list html
</script>
<script type="text/ng-template" id="participantList.html">
Participants list html
</script>
<ul>
<li><a href="#/group">group</a></li>
<li><a href="#/participant">participant</a></li>
</ul>
<div ng-view>
Loading...
</div>
</body>
以下是相关的plunker:http://plnkr.co/edit/K1gMiLenRk0oPkzlGNjv