Angular模块不可用,即使它'

angular module not available even though it's defined

本文关键字:模块 Angular      更新时间:2023-09-26

当我尝试运行下面的代码时,我得到两个错误,说

错误:[$injector:nomod]模块'rooms'不可用!你要么拼错模块名或忘记加载它。如果注册了模块确保您将依赖项指定为第二个论点。

错误:[$injector:modulerr]实例化失败[$injector:nomod]模块'app'不可用!您要么拼错了模块名,要么忘记加载它。如果注册模块时,请确保将依赖项指定为第二个论点。

我可以看到,我没有拼错模块的名字在任何地方,我已经包括了必要的模块的依赖关系,我已经结构化的模块在必要的顺序,使没有模块是未定义的彼此(如在房间。Controllers模块在被注入之前就存在,rooms模块在被注入到app模块之前就存在

(function(){
  'use strict';
  //create the module that is going to contain the controllers for the rooms module
  angular.module('rooms.controllers', [])
    .controller('RoomCtrl', RoomCtrl);
  function RoomCtrl(){
    var vm = this;
    vm.rooms = [];
  };

})();
(function(){
  'use strict';
  //create the rooms module and inject rooms.controllers module and ngRoute module
  angular
    .module('rooms', ['rooms.controllers', 'ngRoute']);
});
(function(){
  'use strict';
  //get the rooms module and config it's routes, because we're getting it we don't need []
  angular
    .module('rooms')
      .config(function($routeProvider){
        $routeProvider
          .when('/rooms',{
            templateUrl:'public/modules/rooms/templates/roomlist.html',
            controller: 'RoomCtrl',
            controllerAs: 'room'
          })
      })
})();

(function(){
  'use strict';
  //bootstrap the whole thing together
  angular.module('app', ['rooms']);
})();

此代码块不执行。

(function(){
    'use strict';
    //create the rooms module and inject rooms.controllers module and ngRoute module
    angular.module('rooms', ['rooms.controllers', 'ngRoute']);
})(); // <-- here