如何为具有多个控制器的angularjs模块实例化$ scope?目前,我收到以下错误消息:
Hibernate:
select
count(*) as y0_
from
person this_
where
this_.born_in_country_name is not null
应用程序的结构如下:
Error: [$injector:modulerr] Failed to instantiate module hello due to:
[$injector:modulerr] Failed to instantiate module navigation due to:
[$injector:modulerr] Failed to instantiate module $scope due to:
[$injector:nomod] Module '$scope' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
模块定义为:
hello
'use strict';
/** * Main module of the application. */
angular
.module('hello', ['ngAnimate', 'ngRoute', 'ngTouch', 'auth', 'home',
'secure', 'public1', 'navigation', 'ui.bootstrap' ])
.config(function ($routeProvider, $httpProvider, $locationProvider) {
//a bunch of config stuff like routeProvider, etc.
})
.run(['$cookies', function($cookies) {
//some startup stuff like setting default values for cookies, etc.
}]);
定义为:
navigation.js
请注意,当我根据下面的评论从注入数组中删除'use strict';
angular
.module('navigation', ['$scope', 'auth', 'modalService', 'ngRoute', 'ngAnimate', 'ui.bootstrap'])
.controller('navigation', function($scope, auth, modalService, $route, $uibModal) {
// code for various stuff,
// including code calling the second controller below
$scope.someVariable = 'some value';
});
// Please note that $uibModalInstance represents a modal window (instance) dependency.
// It is not the same as the $uibModal service used above.
angular.module('navigation', ['$scope', '$uibModalInstance', 'items'])
.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
//code that is controlled by code from the preceding controller
$scope.someOtherVariable = 'some other value';
});
时,我会在$scope
和$scope.someVariable = 'some value'
这两行中收到以下错误:
$scope.someOtherVariable = 'some other value'
答案 0 :(得分:1)
这里有两个你正在混淆的概念。声明角度模块(app)的依赖项列表,并将它们注入控制器或工厂。
使用以下命令声明与模块的依赖关系:
angular.module('myMoudle', ['anotherModule', 'andAnotherModule'])
将它们注入控制器或工厂时,在控制器/工厂功能中完成:
myModule.controller('MyController', ['$scope', 'anotherModule', function($scope, anotherModule) {});
请注意,myModule已经定义,没有理由重新定义它。您也可以使用以下语法访问已定义的模块:
angular.module('myModule', []).controller(...)
注入依赖模块的更高级方法是使用$ inject。 你可以找到更多关于Angular DI here
的信息答案 1 :(得分:0)
初始化控制器有几个错误:
此代码:
angular.module('navigation', ['$scope', '$uibModalInstance', 'items'])
.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items){
//code that is controlled by code from the preceding controller
});
应该按以下方式重写:
angular.module('navigation', [])
.controller('ModalInstanceCtrl', [ '$scope', '$uibModalInstance, 'items',
function($scope, $uibModalInstance, items) { // go with your code here
}]);
所以基本上你必须将与控制器相关的所有注射从模块实例化中移出并将其设置为控制器1。