拿什么拯救我的结果作为索引数组? [英] How to save my result as indexed array?

查看:147
本文介绍了拿什么拯救我的结果作为索引数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的数组卡斯特是空的,但是JSON数据(以及一些元数据包)加载成功,我可以在Chrome网络调试器preVIEW看到的。

如何通过的结果在我的数组,所以有索引,我可以在我的程序访问它们?

  angular.module('卡斯特')。工厂('CustomizingService',['$资源',函数($资源){
    VAR URL =的http://本地主机:31736 / Service1.svc /:链接/';    返回{
        定制:$资源(URL,{回调:JSON_CALLBACK'},{
            顾客: {
                方法:'JSONP',transformResponse:功能(数据){返回angular.fromJson(数据).body.rows},
                PARAMS:{链接:GetCustomers的',numberOf:@numberOf',有效期= @Valid},
                IsArray的:真正},...

我的控制器:

  app.controller('controllerA',['$范围,CustomizingService',
    $ scope.cust = CustomizingService.Customizing.customers({numberOf:12,有效的:真});
}]);


解决方案

我使用AngularJS服务的 restangular 解决了这个问题。这是正确的,容易处理REST API资源的简单方法。

点击此处了解详情: https://github.com/mgonto/restangular

现在我可以降伍资源: - )

新的code:

  app.controller('controllerA',['$范围,CustomizingService','Restangular',函数($范围,CustomizingService,Restangular){
     //http://localhost:31736/Service1.svc/GetCustomers?numberOf=12&valid=true
     Restangular.all('Service1.svc')customGETLIST(GetCustomers的',{numberOf:12,有效的:真})。然后(功能(结果){
        $ scope.customers =结果;
     });
}]);的app.config(功能(RestangularProvider){
    RestangularProvider.setBaseUrl(的http://本地主机:31736 /');
    RestangularProvider.setDefaultRequestParams('JSONP',{回调:
   'JSON_CALLBACK'});
});

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

解决方案

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

这篇关于拿什么拯救我的结果作为索引数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆