1

我正在尝试使用 ngInclude 加载我所有视图共有的标题模板。问题是我正在使用 RequireJS 来延迟加载我的应用程序。

所以我的代码看起来像:

索引.html

<!doctype html>
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" id="ng-app" ng-app=""> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" id="ng-app" ng-app=""> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" ng-app> <!--<![endif]-->
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body ng-app="testApp">

    <header ng-include="views/common/header/header.html"></header>

    <div class="container" ng-view></div>

    <script src="bower_components/requirejs/require.js" data-main="scripts/bootstrap.js"></script>
  </body>
</html>

引导程序.js

require.config({
  paths: {
    angular: '../../bower_components/angular/angular',
    angularRoute: '../../bower_components/angular-route/angular-route',
    angularCookies: '../../bower_components/angular-cookies/angular-cookies',
    angularMocks: '../../bower_components/angular-mocks/angular-mocks',
    text: '../../bower_components/requirejs-text/text'
  },
  shim: {
    'angular' : {'exports' : 'angular'},
    'angularRoute': ['angular'],
    'angularCookies': ['angular'],
    'angularMocks': {
      deps:['angular'],
      'exports':'angular.mock'
    }
  },
  priority: [
    'angular'
  ]
});

//http://code.angularjs.org/1.2.1/docs/guide/bootstrap#overview_deferred-bootstrap
window.name = 'NG_DEFER_BOOTSTRAP!';

require([
  'angular',
  'app',
  'angularRoute',
  'angularCookies'
], function(angular, app, ngRoutes, ngCookies) {
  'use strict';
  var $html = angular.element(document.getElementsByTagName('html')[0]);

  angular.element().ready(function() {
    angular.resumeBootstrap([app['name']]);
  });
});

应用程序.js

/* global controllers:false */
define([
  'angular',
  'controllers/main',
  'common/header/HeaderModule'
], function (angular, MainCtrl, HeaderModule) {
  'use strict';

  return angular.module('testApp', [
    'testApp.controllers.MainCtrl',
    'testApp.Header',

    /*angJSDeps*/
    'ngCookies',
    'ngRoute'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });
});

HeaderModule.js

define([
  'angular',
  'common/menu/menuItems'
], function (angular, MenuItemsController) {
  'use strict';

  angular.module('testApp.Header', [])
      .controller('MenuItemsController',  MenuItemsController);
});

menuItems.js

define(['angular'], function (angular) {
  'use strict';

  angular.module('testApp.menu.controllers.MenuItemsController', [])
    .controller('MenuItemsController', function ($scope) {
      $scope.items = [
        'Menu1',
        'Menu2'
      ];
    });
});

header.html

<nav ng-controller="MenuItemsController">
  <ul>
    <li ng-repeat="item in items"><a href="">{{ item }}</a></li>
  </ul>
</nav>

这没有错误,但也没有显示标题视图 - 我猜是因为模板没有被加载。知道我在这里缺少什么吗?

谢谢

4

1 回答 1

3

事实证明,我只是忘记用单引号包裹路径......

<header ng-include="'views/common/header/header.html'"></header>
于 2014-02-09T17:18:13.437 回答