2013-05-02 57 views
2

我已經以下ngResource定義:無效結果

angular.module('LicenseServices', ['ngResource']). 
    factory('License', function ($resource) { 
     return $resource('api/licenses/:id', { id: '@id' }, { 
      //... 
      'getLicenseTypes': { method: 'GET', url: 'api/licenses/types', isArray: true } 
     }); 
    }); 

GET請求的結果是:

["Trial","Standard"] 

但是,在一個控制器使用該資源:

$scope.licenseTypes = License.getLicenseTypes() 

我收到以下結果:

licenseTypes: 
[ undefined, { 
0: S 
1: t 
2: a 
3: n 
4: d 
5: a 
6: r 
7: d 
} ] 

我在Chrome上使用AngularJS 1.1.4。

我的資源定義有什麼問題?

+1

重複的:http://stackoverflow.com/questions/13813673/one-dimensional-array-of-strings-being-parsed-to-2d-by-angular-resource?rq=1 – Intelekshual 2013-05-02 17:18:57

回答

4

對於像這樣的數據結構,使用$resource確實沒有多大意義。問題是$resource適用於使用所有HTTP動詞的RESTful端點。爲了使這個簡單的AngualarJS擴展傳入的對象與方便的方法($保存,$刪除等)。如果您返回原語,則沒有擴展空間,並且$resource的主要益處之一已消失。

簡而言之:如果你只是在獲取數據之後$ resource是一個過度的IMO,那麼應該使用$http來代替。