Sunday 15 February 2015

angularjs - Configure $ngResource: POST -



angularjs - Configure $ngResource: POST -

i´m newbie using $resource have issue, can´t post info , response "options" , "404".

could tell me wrong? thanks.

//model side app.factory("contact", function ($resource) { //$resource(url,[paramdefaults], [actions], options); homecoming $resource( api_url + "/client/:idclient/contact/:idcontact", { idclient: '@idclient', idcontact: '@idcontact'}, { 'query': {method: 'get', isarray: true}, 'newcontact': {method: 'post',params: {idclient: '@idclient'}}, 'getcontact':{method: 'get', isarray: false} } ); }); //controller side var contact = {"name":"mary", "lastname":"kurry","phone":"1234324234"}; contact.newcontact({'idclient':idclient}, contact);

you can utilize simple way:

contact contact = {"name":"mary", "lastname":"kurry","phone":"1234324234"}; contact.$newcontact({'idclient':idclient});

see more @ example:https://docs.angularjs.org/api/ngresource/service/$resource

var creditcard = $resource('/user/:userid/card/:cardid', {userid:123, cardid:'@id'}, { charge: {method:'post', params:{charge:true}} }); // can retrieve collection server var cards = creditcard.query(function() { // get: /user/123/card // server returns: [ {id:456, number:'1234', name:'smith'} ]; var card = cards[0]; // each item instance of creditcard expect(card instanceof creditcard).toequal(true); card.name = "j. smith"; // non methods mapped onto instances card.$save(); // post: /user/123/card/456 {id:456, number:'1234', name:'j. smith'} // server returns: {id:456, number:'1234', name: 'j. smith'}; // our custom method mapped well. card.$charge({amount:9.99}); // post: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'j. smith'} });

angularjs rest angular-resource

No comments:

Post a Comment