为什么我在此 angular 应用程序代码中得到 "Incorrect injection token!"?

Why am I getting "Incorrect injection token!" in this angular application code?

我正在尝试使用以下代码通过 AngularJS 设置 restful API 界面:

'use strict';

(function(angular) {

  function ApiAction($resource, ResourceParameters) {
    return $resource(ResourceParameters.route,
      { },
      { api_index: {
        method: ResourceParameters.method,
        isArray: true 
        }
      });

    return $resource(ResourceParameters.route,
      { },
      { create: {
        method: ResourceParameters.method,
        isArray: true
        }
      }
                    );
  }

  function ResourceParameters($scope) {
    var factory = {};
    factory.method = '';
    factory.route = '';
    factory.SetMethod = function(method) {
      factory.method = method;
    }
    factory.SetRoute = function(route) {
      factory.route = route;
    }
    return factory;
  }

  function superheroCtr($scope, ApiAction, ResourceParameters) {
    $scope.superheroSubmit = function() {
     // ApiAction.create({}, { superhero_name: $scope.superheroName, age: $scope.superheroAge });
      angular.forEach($scope.superheroes, function(hero) {
  //      ApiAction.create({}, { superhero_name: hero.superhero_name, age: hero.age }); 
      });
    };
    var heroesResources = ResourceParameters($scope).SetRoute('/api/');
    var heroes = ApiAction.api_index({}, heroesResources);
    $scope.superheroes = [];
    heroes.$promise.then(function(data) {
      angular.forEach(data, function(item) {
        $scope.superheroes.push(item);
      });
    }, function(data) {
      //if error then...
    });

    $scope.appendSuperheroFields = function() {
      var i = $scope.superheroes.length + 1;
      $scope.superheroes.push({"id": i, age: "", superhero_name: "" })
    }

  }

  var superheroApp = angular.module('superheroApp', ['ngResource']);
  superheroApp.controller('superheroCtr', ['$scope', 'ApiAction', 'ResourceParameters', superheroCtr]);
  superheroApp.factory('ResourceParameters', ['$scope', ResourceParameters]);
  superheroApp.factory('ApiAction', ['$resource', ResourceParameters, ApiAction]);

})(angular);

然而,当我 运行 它时,我得到以下错误:

错误:[$injector:itkn] 注入令牌不正确!预期服务名称为字符串,得到函数 ResourceParameters($scope)

这是为什么?

Simply you can not inject $scope OR you can not have access to $scope inside a factory

你的问题出在这一行

superheroApp.factory('ResourceParameters', ['$scope', ResourceParameters]);

您需要将该行替换为

superheroApp.factory('ResourceParameters', [ResourceParameters]);

工厂

function ResourceParameters() { //<--removed $scope from here
    var factory = {};
    factory.method = '';
    factory.route = '';
    factory.SetMethod = function(method) {
      factory.method = method;
    }
    factory.SetRoute = function(route) {
      factory.route = route;
    }
    return factory;
}

更新

此外,您应该更正 ApiAction 的声明,其中 ResourceParameters 应该放在 ' single qoutes

superheroApp.factory('ApiAction', ['$resource', 'ResourceParameters', ApiAction]);