javascript - Not able to post data angular ng-resource -
i want persist data using post method throught angular ngresource. have written below code process. but, when try persist error coming.
js code
angular.module('app').factory("testservice", function($resource) { return{ addcount: $resource('/save/count',{method:'post',params:{ date: '@date', count: '@count'}} ) }; });
//controller code
$scope.add = function(){ testservice.addcount().get({ date: new date(formatteddate).tostring("mm/dd/yyyy"), count: count},function(response){ console.log('success') });};
as click add() html getting below error. typeerror: cannot read property 'get' of undefined
kindly,provide suggestion overcome issue
restful services should on per entity basis. route should /count
or api/count
depends on how set api endpoints.
then when use $resource
, point @ endpoint , let $resource
handle posting etc.
the $resource
follows meanings behind http verbs. get
should data, post
should save data, put
should update, delete
should delete etc.
$scope.add = function(){ var count = $resource('/count'); count.save({ date: new date(formatteddate).tostring("mm/dd/yyyy"), count: count }, function() { // success }); };
that how see myself using $resource
. can abstract out creation of count
service created.
$resource
so when call $resource('some/route');
return object containing these methods:
{ 'get': {method:'get'}, 'save': {method:'post'}, 'query': {method:'get', isarray:true}, 'remove': {method:'delete'}, 'delete': {method:'delete'} };
as can see, save
post
method. call take reference $resource('some/route');
like:
var count = $resource('some/route');
then call method.
for http "class" actions: resource.action([parameters], [success], [error])
for non-get "class" actions: resource.action([parameters], postdata, [success], [error])
for non-get instance actions: instance.$action([parameters], [success], [error])
so understand it, class means reference count
, instance means when class has been executed , you've recieved object. object instance
Comments
Post a Comment