类型错误:api.getAll 不是函数,无法识别服务方法 [英] TypeError: api.getAll is not a function, service method is not recognized

查看:26
本文介绍了类型错误:api.getAll 不是函数,无法识别服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的服务和一个试图从中获取一些信息的控制器,但我一直收到 .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();

      }]);

}());

但我收到错误:

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

感谢您的帮助.

推荐答案

你已经调换了controller工厂函数中的依赖顺序,顺序必须与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.

代码

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

这篇关于类型错误:api.getAll 不是函数,无法识别服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆