我有一个服务,并且声明这样的控制器:
angular
.module('purchaseApp')
.service('sharedProperties', function() {
var stringValue = 'test string value';
return {
getString: function() {
return stringValue;
}
}
})
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
当我尝试在控制器中访问该服务时,它会将我返回"未定义",但我不知道为什么...您能帮我吗?
谢谢。
您的控制器缺少sharedProperties
。您需要注入它。添加$http
。
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', 'sharedProperties', function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', **'sharedProperties'**, function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
在您的controller
依赖参数中也添加共享Properties服务。