为什么 Angular 说我 setData 不是一个函数?
angular.module('mdl').factory('DataService', ['$http', '$cookieStore', '$rootScope',
function ($http, $cookieStore, $rootScope) {
return {
setData: function (data) {
$rootScope.data = data;
},
};
}
]);
这是我的 Controller ,它调用 setData
。
angular.module('mdl').controller('DataCtrl', ['$scope', '$http', '$location', '$rootScope', 'DataService',
function($scope, $http, $location, DataService) {
$scope.getData = function (id) {
$http.post('/rest/data/get', id)
.success(function (data, status, headers, config) {
DataService.setData(data);
$location.path('/main');
})
.error(function (data, status, headers, config) {
});
};
}
]);
最佳答案
依赖注入(inject)存在问题,您声明了 6 个项目,而在您的函数中您有 5 个项目。而缺少的是 DataService。
['$scope', '$http', '$location', '$rootScope', 'DataService',
function($scope, $http, $location, $rootScope /*Missing*/, DataService) {
…
}
关于javascript - Angular : service. setData 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30595371/