我编写了一个基本的 Angular JS Controller 和要注入(inject)的服务,代码如下。
Controller
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;
}]);
据我所知,这应该可行。但是在控制台中我可以看到错误
Exception was thrown at line 5, column 9 in http://localhost:58270/app/SEFlex/controllers/SEFlexHomeController.js 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'getCloudConfigParams'
我认为我正在做一些基本的错误,但我看不出是什么。谁能发现问题吗?
最佳答案
您忘记注入(inject)一项服务 (JobService
):
app.controller("SEFlexHomeController", ["$scope", "$http", "$modal", "$log", "$element", "JobService", "$rootScope", "SEApplicationService", function ($scope, $http, $modal, $log, $element, JobService, $rootScope, SEApplicationService) {
您有内联类型提示,但没有在 Controller 函数中注入(inject)的参数。
关于javascript - 在 Angular 服务中找不到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33020087/