1

我正在尝试包含多个要包含在 中的部分模板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脚本指令?

4

1 回答 1

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>

这是相关的插件:http: //plnkr.co/edit/K1gMiLenRk0oPkzlGNjv

于 2013-05-07T01:01:21.053 回答