如何使用 $resource 将数组从 angular js 传递给 REST 服务 [英] how to pass an array from angular js to REST service using $resource
问题描述
我是 AngularJs 和 REST 服务的新手,想知道如何使用 $resource 将对象数组传递给 Angular js 中的 REST-POST 调用.我知道如何传递单个参数但不确定如何传递数组.这是我传递单个参数的 Get 调用代码.谁能告诉我,我怎样才能用 POST + 数组实现同样的事情.谢谢!!
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'} }
} );
});
推荐答案
在你的控制器中,你需要做这样的事情.您将 POST 请求作为 save()
函数的参数传递的位置.工作 jsFiddle.您可以使用网络下的 Chrome 开发工具验证这一点.
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.
看看 AngularJS $resource 文档,有一些例子如何对 API 进行 POST 调用.
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!";
});
}]);
这篇关于如何使用 $resource 将数组从 angular js 传递给 REST 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!