无法通过$ resource

时间:2015-06-25 15:14:27

标签: javascript angularjs angularjs-service angular-resource

无法通过$resource获取数组。你能帮助我吗?当我使用$http时,一切都很好

我在控制台中有错误:

TypeError: undefined is not a function
at http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:597:29
at forEach (http://127.0.0.1:9000/bower_components/angular/angular.js:327:18)
at angular.module.provider.$get.Resource.(anonymous function).$http.then.value.$resolved (http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:595:19)
at deferred.promise.then.wrappedCallback (http://127.0.0.1:9000/bower_components/angular/angular.js:11616:81)
at http://127.0.0.1:9000/bower_components/angular/angular.js:11702:26
at Scope.$get.Scope.$eval (http://127.0.0.1:9000/bower_components/angular/angular.js:12797:28)
at Scope.$get.Scope.$digest (http://127.0.0.1:9000/bower_components/angular/angular.js:12609:31)
at Scope.$get.Scope.$apply (http://127.0.0.1:9000/bower_components/angular/angular.js:12901:24)
at done (http://127.0.0.1:9000/bower_components/angular/angular.js:8487:45)
at completeRequest (http://127.0.0.1:9000/bower_components/angular/angular.js:8703:7)

我用方法

创建了一个工厂
coeffsResource.factory("CoeffsResources",['$resource', 

function($resource) {
  return $resource('/api/:action',{}, {
    get_all_coeffs: { method:'GET', isArray:false, params: {action: 'getAllRegionCoefficients'} },
    save_all_coeffs: { method:'POST', params: {action: 'storeAllRegionCoefficients'} },
    get_manufacturer: { method: 'GET', isArray:true, params: {action: 'getAllManufacturers'} },
    get_models: { method: 'GET', params: {action: 'getModels'} },
    get_classes: {method: 'GET', params: {action: 'getClassesConfig'} },
    get_regions: {method: 'GET', params: {action: 'getAllRegions'} },
    get_ages_config: {method: 'GET', params: {action: 'getAgesConfig'} },
    get_odometer: {method: 'GET', params: {action: 'getOdometersConfig'} },
    get_tax_config: {method: 'GET', params: {action: 'getTaxConfig'} }
  }, {stripTrailingSlashes: false})
}]);

在控制器中包含工厂

angular.module('etachkaEvaluatorFrontendApp')
  .controller('CoeffCtrl', function($scope,  $http, $resource, $q, CoeffsResources) {

      var coeffsResourcesObject =  new CoeffsResources();
      coeffsResourcesObject.$get_manufacturer().then(function() {

      }, function() {

      })
})

2 个答案:

答案 0 :(得分:1)

你为什么要新单身? AngularJS工厂不打算以这种方式工作。有关详细信息,请参阅AngularJS service docs

  

Angular服务是:

     
      
  1. Lazily实例化 - Angular仅在实例化时实例化服务   应用程序组件取决于它。

  2.   
  3. 单身人士 - 每个组成部分   依赖于服务获取对单个实例的引用   由服务工厂生成。

  4.   

null中的使用情况更改为以下内容...(这也假设您已在应用程序的某个早期点正确加载了ngResource模块)

myConsole.writer().print(playerOptions); //This is not right.

为了更好地了解工厂行为,我制作了两个简单的演示。请注意,这些不是为了以复制/粘贴方式解决您的问题 - 而是为了演示当我们CoeffCtrl AngularJS工厂时会发生什么。

JSFiddle Link - 演示 - 更正

JSFiddle Link - 演示 - 错误 - .controller('CoeffCtrl', function($scope, $http, $resource, $q, CoeffsResources) { CoeffsResources.$get_manufacturer().then(function() { }, function() { })

答案 1 :(得分:1)

我认为您需要注入ngResource依赖项。

angular.module('etachkaEvaluatorFrontendApp', ['ngResource'])