我有一个非常简单的服务,一个控制器试图从中获取一些信息,但是我一直在获取.methodName不是一个函数。
这是服务apiService.js:
(function (module) {
function api() {
var sharedService = {};
sharedService = {
getAll: function () {
return 'test';
}
};
return sharedService;
}
module.factory("api", api);
}(angular.module("anbud")));
然后,我尝试在控制器中使用我的方法getAll,如下所示:
(function () {
'use strict';
angular.module('anbud')
.controller('BasicExampleCtrl', ['$scope', 'api', function (api, $scope) {
// on successfull request
function onJson(json) {
$scope.data = json;
}
// error getting json
function onError() {
throw 'error getting json';
}
function initialize() {
api.getAll()
.then(onJson, onError);
}
initialize();
}]);
}());
但是我得到了错误:
TypeError: api.getAll is not a function
at initialize (basic-example.js:67)
任何帮助表示赞赏,谢谢。
您已经在controller
工厂函数内部交换了依赖项序列,该序列必须与DI array
注入函数时所包含的序列相同。
代码
.controller('BasicExampleCtrl', ['$scope', 'api', function ($scope, api) { //<- first $scope then api.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句