$resource
Create a Resource object's factory function that allows you to interact securely with the resful server.
Installation
Ngresource module is an optional ANGULARJS module, if necessary, we have to quote JS separately
<script type= "Text/javascript" src= "/javascripts/angular-resource.js" >
$resource Application
We do not communicate directly with the server through the $resource service itself, $resource is a factory that creates resource objects to create objects that interact with the service side.
var User = $resource ('/api/users/:userid ', {userId: ' @id '});
The returned user object contains a way to interact with the backend service, and we can interpret the user object as an interface to interact with the RESTful backend service.
The object contains methods of two get types that have three non-get types.
User.get ({ID: ' 123 '}, SUCCESSFN, ERRORFN);
The method sends a GET request to the URL and expects a response of the JSON type. This will send a request to/api/users/123, SUCCESSFN processing request successfully response, ERRORFN processing error.
User.query (params, SUCCESSFN, ERRORFN)
get()
similar to the method used, is generally used to request more than one piece of data.
Save (params, payload, SUCCESSFN, ERRORFN);
The Save method initiates a POST request, where the params parameter fills the variable in the URL, and the object payload is sent as the request body
Delete (params, payload, SUCCESSFN,ERRORFN)
Delete Method A delete request, payload to be sent as a message body
Remove (params, payload, SUCCESSFN, ERRORFN)
Similar to delete, except that the remove is used to remove more than one piece of data
By $resource the generated objects to interact with the server, we see the function of processing success and failure, which accepts parameters that are not simply objects, but are packaged objects that are added, $save()
$remove()
$delete
three methods, You can call these three methods directly to interact with the backend server.
User.get ({ID: ' 123 '}, function (user) {
user.name = ' changeanothername ';
User. $save ();
This is equivalent to User.save ({ID: ' 123 '},{name: ' Changeanothername '})
});
$resource extension
$resource encapsulate five of common requests, we can also extend $resource.
To extend $resource here we need to pass in the third argument, which is an object.
$resource ('/api/users ', {},{
sendemail:{method
: ',
URL: ',
params:{},
Isarray:boolean,
transformrequest: Function or function array
transformresponse: function or function array
cache: Boolean or Cached Object
timeout: Numeric or Promise Object
withcredentials: Boolean type
Responsetype: string, used to set Xmlhttprequestresponsetype property
}
})
We can also use the $resource service as the basis for custom services.
Angular.module (' testApp ', [' Ngresource ']), factory (' UserService ', [' $resource ', function ($resource) {return
$ Resource (url,{},{});
Summarize
The above is about ANGULARJS in $resource and RESTFU service-side data interaction of the full content, I hope this article for you to learn or use ANGULARJS can help, if you have questions you can message exchange.