我有一个模块和两个控制器:
var module = angular.module("app", ["agGrid", "ngAnimate", "ngSanitize", "ngDialog"])
module.controller("mainCtrl", ["$scope", "dataService","$timeout","dateFilter","ngDialog", "$http", function ($scope, $http, $timeout, dateFilter, ngDialog, dataService) {
}]);
module.controller("modalCtrl", ["$scope", "ngDialog", "dataService", function ($scope, ngDialog, dataService) {
$scope.printEntity = function () {
console.log(dataService.getEntityArray());
}
}]);
一项服务:
module.factory("dataService", function () {
var entityArrayService = [];
return {
setEntityArray: function (entityArray) {
entityArrayService = entityArray;
},
getEntityArray: function () {
return entityArrayService;
}
};
});
我可以从我的SECOND控制器内部调用dataService.setEntityArray(array)
,但当我尝试从我的第一个控制器调用它时,它会告诉我dataService.setEntityArray is not a function
答案 0 :(得分:2)
依赖注入的顺序不正确。控制器函数中的参数必须重复数组中元素的顺序。在你的情况下,dataService
是第二个参数:
module.controller("mainCtrl", ["$scope", "dataService","$timeout","dateFilter","ngDialog", "$http", function ($scope, dataService, $timeout, dateFilter, ngDialog, $http) {
}]);
答案 1 :(得分:1)
第一个控制器定义中的变量顺序无效。它应该是:
function ($scope, dataService, $timeout, dateFilter, ngDialog, $http)