how to pass an array from angular js to REST service using $resource [duplicate]

青春壹個敷衍的年華 提交于 2019-12-01 11:23:36

问题


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

回答1:


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!";
    });

}]);


来源:https://stackoverflow.com/questions/21393800/how-to-pass-an-array-from-angular-js-to-rest-service-using-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!