javascript - $resource delete method doesn't work right -
i learning angluarjs's $resource. set button on html deleting record json file,and hope delete 1 record testform.json file. unfortunately, failed , can't solve problem.
the codes here: my angularjs code deleting function in controller codes shown follows
$scope.deleterecord = function () { var id = 1; var arecord = new singleresource(); arecord.$get({userid:id},function () { $log.info(arecord); arecord.$delete(); });
the html part of deleting function shown here:
<div class="form-group"> <div class="col-sm-6 col-sm-push-2"> <button type="button" class="btn btn-primary" ng-click="deleterecord()">delete information</button> </div> </div>
"$scope.deleterecord" function when clicking delete information button, record id=1 in json file should removed. error says:"delete http://localhost:3000/contactinfo 404 (not found) :3000/contactinfo:1 ", while "arecord" shown object in log "m {email: "333@333", password: 321, method: "internet", id: 1, $promise: undefined…}", should fine.
$http
$http.get('/someurl', config).then(successcallback, errorcallback);
$http.post('/someurl', data, config).then(successcallback, errorcallback);
$http built angular, there’s no need overhead of loading in external dependency. $http quick retrieval of server-side data doesn’t need specific structure or complex behaviors. it’s best injected directly controllers simplicity’s sake.
$resource
the action methods on class object or instance object can invoked >with following parameters:
http "class" actions: resource.action([parameters], [success], [error])
non-get "class" actions: resource.action([parameters], postdata, [success], [error])
$resouce situations more complex $http. it’s when have pretty structured data, plan of crunching, relationships, , other operations on server side before delivering api response. $resource doesn’t let once data javascript app, should deliver app in final state , make more rest calls when need manipulate or @ different angle.
alternatively use: restangular https://github.com/mgonto/restangular
restangular perfect option complex operations on client side. lets attach custom behaviors , interact data in same way other model paradigms you’ve used in past.
angularjs guide: https://docs.angularjs.org/api/ng/service/$http#delete
try delete method:
$scope.deleterecord = function () { var id = 1; var arecord = new singleresource(); arecord.$delete({userid:id},function () { $log.info(arecord); //arecord.$delete(); });
or
$http({ method: 'delete', url: '/someurl' }).then(function successcallback(response) { // callback called asynchronously // when response available }, function errorcallback(response) { // called asynchronously if error occurs // or server returns response error status. });
i hope helps.
Comments
Post a Comment