简单的requireJS with angularJS - angular未定义

时间:2013-10-24 19:58:13

标签: angularjs requirejs

我是requireJS的新手,我希望得到一个简单的启动'hello worldish'项目启动并运行。我错过了一些东西,因为当angular is not defined试图加载时我得到GreetCtrl作为JS错误。

的index.html:

<!DOCTYPE html>
<html ng-app="ReqApp" ng-controller="GreetCtrl">
  <body>
    <h1>{{greeting}}!</h1>
    <script src="assets/require/require.js" data-main="assets/require/main"></script>
  </body>
</html>

main.js:

require.config({
    // alias libraries paths
  paths: {
    'domReady':      'domReady',
    'angular':       '../../vendor/angular/angular.min',
    'GreetCtrl':     '../../src/app/modules/GreetCtrl',
    'app':           '../../src/app/app'
  },
  // angular does not support AMD out of the box, put it in a shim
  shim: {
    'angular': {
      exports: 'angular'
    }
  },
  // kick start application
  deps: ['./bootstrap']
});

bootstrap.js:

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

    require(['domReady!'], function (document) {
        ng.bootstrap(document, ['ReqApp']);
    });
});

app.js:

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

  return ng.module('ReqApp', [
    'ReqApp.GreetCtrl'
  ]);
});

最后,GreetCtrl.js:

angular.module( 'ReqApp.GreetCtrl', [])
.controller( 'GreetCtrl', function GreetCtrl ($scope) {
  $scope.greeting = "greetings puny human";
});

根据萤火虫,GreetCtrl.js的第1行引发错误angular is not defined。我在这里缺少什么?

提前致谢!!

1 个答案:

答案 0 :(得分:2)

您尚未告知GreetCtrl它取决于angular,因此它是undefined。尝试将GreetCtrl更改为此类

define(['angular'], function(angular){
    angular.module( 'ReqApp.GreetCtrl', [])
    .controller( 'GreetCtrl', function GreetCtrl ($scope) {
         $scope.greeting = "greetings puny human";
    });
});