我有一個模塊,兩個控制器:角服務可以從一個控制器設置的數據而不是其他
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;
}
};
});
我可以從我的第二個控制器內部調用dataService.setEntityArray(array)
,但是當我嘗試從我的第一個控制器調用它時,它告訴我dataService.setEntityArray is not a function
你在開玩笑吧,順序很重要!?謝謝!將在時間用完時接受。 – xeon48
當然:)這就是數組符號的要點,以便Angular知道什麼參數對應於什麼服務。所以,當它是'['dataService','$ http',函數(a,b){}]''Angular知道'a'是'DataService'等時縮小。 – dfsq
哦,我明白了!謝謝你的幫助! – xeon48