2015-06-25 43 views
2

得到陣列無法通過$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() { 

     }) 
}) 
+0

請添加您收到任何錯誤消息。 – JackWhiteIII

+0

Firts代碼塊在我的問題,這是我在控制檯中的錯誤 –

+0

對不起,我完全誤解了。我的錯。 – JackWhiteIII

回答

1

爲什麼你興起一個單身? AngularJS工廠不打算以這種方式工作。見AngularJS service docs以獲取更多信息

角度的服務宗旨是:

  1. 懶洋洋地實例化 - 角只有當 應用程序組件取決於它實例化的服務。

  2. 單件 - 依賴於服務的每個組件 獲取服務工廠生成的單個實例 的引用。

CoeffCtrl更改您使用以下...(這也假設你已經正確地加載在一些較早點ngResource模塊在應用程序中)

.controller('CoeffCtrl', function($scope, $http, $resource, $q, CoeffsResources) { 
    CoeffsResources.$get_manufacturer().then(function() { 

    }, function() { 

    }) 

對於更好地瞭解工廠行爲我製作了兩個簡單的演示。請注意,這些不是爲了解決在複製/粘貼方式的問題 - 而是說明,當我們new的AngularJS工廠發生了什麼。

JSFiddle Link - 演示 - 正確

JSFiddle Link - 演示 - 不正確 - TypeError: undefined is not a function

+1

它不工作太... 當我期待網絡在devtools,我得到一個迴應... 函數處理程序不起作用(( 而我在其他項目中使用「新」,這是很好的( –

1

我認爲你需要注入ngResource依賴。

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

不錯的發現,我忽略了這一點,我有一種感覺,這個問題有幾個問題,我們正在識別每個反饋將是必要的 – scniro

+0

我已經做到了...... –

+1

@AndrewMedvedsky做了什麼? – Donal