类型错误:api.getAll是不是一个功能,服务方法无法识别错误、无法识别、类型、功能

2023-09-13 05:17:28 作者:黑米和小志@

我有一个非常简单的服务和控制器试图从它那里得到一些信息,但我不断收到.methodName不是一个函数。

I have a very simple service and a controller attempting to get some information from it, but I keep getting .methodName is not a function.

下面是服务, apiService.js

(function (module) {

    function api() {

        var sharedService = {};

        sharedService = {

            getAll: function () {
                return 'test';
            }
        };
        return sharedService;

    }

    module.factory("api", api);


}(angular.module("anbud")));

然后我尝试使用我的方法GETALL控制器,就像这样:

Then I attempt to use my method getAll in the controller, like this:

(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();

      }]);

}());

但我得到的错误:

But I get the error:

TypeError: api.getAll is not a function
    at initialize (basic-example.js:67)

任何帮助是AP preciated谢谢。

Any help is appreciated thank you.

推荐答案

您已经互换,因为它们都包含控制器工厂函数里面的依赖序列,序列必须是相同的在 DI阵列,而在功能注入。

You have interchanged the dependencies sequence inside controller factory function, the sequence must be same as they are included in DI array while injecting in function.

code

.controller('BasicExampleCtrl', ['$scope', 'api', function ($scope, api) { //<- first $scope then api.