AJ_83
AJ_83

Reputation: 299

How to save my result as indexed array?

Currently my array cust is empty but the json data (wrapped with some metadata) is successfully loaded as I can see in Chrome debugger network preview.

How to pass the result in my array, so that there are indexed and I can access them in my program?

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 },...

My controller:

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

Upvotes: 2

Views: 412

Answers (2)

AJ_83
AJ_83

Reputation: 299

I solved the problem by using the AngularJS service restangular. It is an easy way to handle Rest API Resources properly and easily.

More information here: https://github.com/mgonto/restangular.

Now I can drop ng-resource :-)

The new code:

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'});
});

Upvotes: 1

Nicol
Nicol

Reputation: 234

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

Upvotes: 2

Related Questions