如何使用$资源通过从角JS数组REST服务 [英] how to pass an array from angular js to REST service using $resource

查看:102
本文介绍了如何使用$资源通过从角JS数组REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,AngularJs和REST服务,并想知道我可以在使用$资源角度JS传递对象的数组REST-POST调用。
我知道如何通过一个单一的参数,但不知道如何传递一个数组。这是我的code为获取调用哪里我传递一个参数。谁能告诉我,我怎么能实现与POST +阵列同样的事情。谢谢!

  VAR的服务= angular.module('myApp.services',['ngResource']);
        services.factory('AngularIssues',
            功能($资源){
            返回$资源(的http://本地主机:8181 / MyRESTService /服务/的UserInfo /:身份证',{},{
                得到:{方法:GET,则params:{ID:'@id'}}
             });
        });


解决方案

在您的控制器,您需要做这样的事情。你在哪里通过你的POST请求的的保存()函数的参数。工作的jsfiddle 。您可以通过网络下的Chrome浏览器的开发者工具验证这一点。

有一个看 AngularJS $资源文档,也有一些例子如何使一个POST调用API。

  services.factory('AngularIssues',
功能($资源){
   返回$资源('/回声/ JSON /',{},{
       得到:{方法:GET,则params:{ID:'@id'}}
   });
});services.controller('AppController的',['$范围,AngularIssues',
功能($范围,AngularIssues){    AngularIssues.save({theArray:[{名:对象1},{名字:对象2}]})
    。$ promise.then(功能(RES){
       $ scope.done =我完成了!
    });}]);

I am a novice to AngularJs and REST service and want to know how can I pass an array of object to REST-POST call in angular js using $resource. I know how to pass a single parameter but not sure how to pass an array. This is my code for Get call where I am passing a single parameter. Can anyone tell me , how can I achieve the same thing with POST + array. Thanks!!

 var services = angular.module('myApp.services', ['ngResource']);
        services.factory('AngularIssues',
            function($resource){
            return $resource('http://localhost:8181/MyRESTService/services/UserInfo/:id', {} ,{
                get:{method:'GET' , params: {id: '@id'} }
             } );
        });

解决方案

In your controller you'll need to do something like this. Where you are passing your POST request as a parameter of the save() function. Working jsFiddle. You can verify this using Chrome's Dev Tools under Networks.

Have a look at the AngularJS $resource documentation, there are some examples of how to make a POST call to the API.

services.factory('AngularIssues',
function($resource){
   return $resource('/echo/json/', {} ,{
       get:{method:'GET' , params: {id: '@id'} }
   } );
});

services.controller('AppController', ['$scope', 'AngularIssues', 
function($scope, AngularIssues) {

    AngularIssues.save({ "theArray": [{ name: "Object 1" }, { name: "Object 2" }] })
    .$promise.then(function(res) {
       $scope.done = "I AM DONE!";
    });

}]);

这篇关于如何使用$资源通过从角JS数组REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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