我有一个服务和一个 Controller ,声明如下:
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());
});
当我尝试访问 Controller 中的服务时,它返回“未定义”,但我不知道为什么......你能帮助我吗?
谢谢。
最佳答案
您的 Controller 缺少sharedProperties
。你需要注入(inject)它。将其添加到 $http
之后。
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', 'sharedProperties', function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
关于javascript - Controller 中未定义的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30212722/