2015-05-22 31 views
2

目前我的數組cust是空的,但json數據(包含一些元數據)已成功加載,因爲我可以在Chrome調試器網絡預覽中看到。如何將我的結果保存爲索引數組?

如何在我的數組中傳遞結果,以便有索引,並且可以在我的程序中訪問它們?

angular.module('Cust').factory('CustomizingService', ['$resource', function ($resource) { 
    var url = 'http://localhost:31736/Service1.svc/:link/'; 

    return { 
     Customizing: $resource(url, {callback: 'JSON_CALLBACK'}, { 
      customers: { 
       method: 'JSONP', transformResponse: function(data) {return angular.fromJson(data).body.rows}, 
       params: { link: 'GetCustomers', numberOf: '@numberOf', valid = @valid }, 
       isArray: true },... 

我的控制器:

app.controller('controllerA', ['$scope', 'CustomizingService', 
    $scope.cust = CustomizingService.Customizing.customers({numberOf: 12, valid: true}); 
}]); 

回答

1

我使用AngularJS服務restangular解決了這個問題。這是一種輕鬆正確處理Rest API資源的簡單方法。

更多的信息在這裏:https://github.com/mgonto/restangular

現在我可以降伍資源:-)

新代碼:

app.controller('controllerA', ['$scope', 'CustomizingService', 'Restangular', function($scope, CustomizingService, Restangular) { 
    //http://localhost:31736/Service1.svc/GetCustomers?numberOf=12&valid=true 
    Restangular.all('Service1.svc').customGETLIST('GetCustomers',{numberOf: 12, valid: true}).then(function(result){ 
     $scope.customers= result; 
    }); 
}]); 

app.config(function(RestangularProvider){ 
    RestangularProvider.setBaseUrl('http://localhost:31736/'); 
    RestangularProvider.setDefaultRequestParams('jsonp', {callback: 
    'JSON_CALLBACK'}); 
}); 
2
app.controller('controllerA', ['$scope', 'CustomizingService', function(){ 
    CustomizingService.Customizing 
    .customers({numberOf: 12, valid: true}).$promise 
    .then(function(data){ 
     $scope.cust = data; 
    },function(error){ 
    }); 
}]); 
+0

得到的錯誤:。然後是不是一個函數 –

+0

對不起,我已經編輯,你可以試試再次 – Nicol

+0

試過了,但是我在chrome調試器中檢查$ scope時沒有創建cust對象。 –

相關問題