控制器中未定义的服务

Undefined service in 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());
});

当我尝试访问控制器中的服务时,它 returns 我 "undefined",但我不知道为什么...你能帮我吗?

谢谢。

您的控制器丢失了 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 依赖参数中添加 sharedProperties 服务。