在Angular Service中找不到方法

时间:2015-10-08 15:31:33

标签: javascript asp.net angularjs asp.net-mvc

我已经编写了一个基本的角度JS控制器和服务来注入,代码如下。

控制器

app.controller("SEFlexHomeController", ["$scope", "$http", "$modal", "$log", "$element", "JobService", "$rootScope", "SEApplicationService", function ($scope, $http, $modal, $log, $element, $rootScope, SEApplicationService) {
        $scope.isDataLoading = false;
        $scope.configvalues = SEApplicationService.getCloudConfigParams();
    }
]);

服务

app.factory("SEApplicationService", ["$log", "$http", "$timeout", function($log, $http, $timeout) {

    var appService = {};    

    appService.getCloudConfigParams = function () {
        return $http.post("/SEFlex/SEFlexAdmin/GetCloudConfigValues");
    }

    return appService;
}]);

据我所知,这应该有效。但是在控制台中我可以看到错误

  

在第5行第9行引发异常   http://localhost:58270/app/SEFlex/controllers/SEFlexHomeController.js   0x800a01b6 - JavaScript运行时错误:对象不支持属性   或方法'getCloudConfigParams'

我认为我做了一些基本的错误,但我看不出是什么。有谁能发现这个问题?

2 个答案:

答案 0 :(得分:2)

您忘了注入一项服务(JobService):

app.controller("SEFlexHomeController", ["$scope", "$http", "$modal", "$log", "$element", "JobService", "$rootScope", "SEApplicationService", function ($scope, $http, $modal, $log, $element, JobService, $rootScope, SEApplicationService) {

您有内联类型提示,但没有在控制器函数中注入的参数。

答案 1 :(得分:2)

你错过了正确注入JobService。纠正 -

app.controller("SEFlexHomeController", ["$scope", "$http", "$modal", "$log", "$element", "JobService", "$rootScope", "SEApplicationService", function ($scope, $http, $modal, $log, $element, JobService, $rootScope, SEApplicationService) {
        $scope.isDataLoading = false;
        $scope.configvalues = SEApplicationService.getCloudConfigParams();
    }
]);