This article mainly introduces relevant content about the interaction of $resource data. It is shared for your reference and learning. Let’s take a look at the detailed introduction together:
$resource
Create a factory function of the resource object, which allows you to safely interact with the RESFUL server.
ngResource module is required.angular-resource[.min].js
By default, the end slash (behavior that can cause undesired behavior of the backend server) will be stripped from the calculated URL.
This can be configured via $resourceProvider:
(["$resourceProvider",function($resourceProvider){ $ = false; }])
rely:$http
use:$resource(url,[paramDefaults],[actions],options);
url:A parameterized url template with prefix parameters (such as: /user/:username). If you are using a URL with a port number (such as::8080/api), you need to consider it carefully. If with a suffix (such as: / or /: or /resource/:resource_id.:format). If the argument before the suffix is empty, in this case: resource_id is executed first than /., if you need this sequence to appear without crashing, then you can avoid it by /\.
paramDefaults:The default values of the url parameters, these can be overridden in the method. If any value of the parameter is a function, it will be executed as the parameter value obtained for each request (unless the parameter is ignored).
Each key-value pair in the parameter object is bound to a url template first, and any extra keys are attached to the "?" of the url query./path/:verb +{verb:'greet',salutation:'hello'} => /path/greet?salutation=hello
actions: The hash of a custom configuration that the user extends to the default settings of resource behavior, which will be created in the format of $.
action: String, name of action, this name will become the name of the resource object method.
method: string, http method (case insensitive, such as GET, POST, PUT, DELETE, JSONP, etc.).
params: object, preset parameters for this action. If the value of any parameter is a function, a parameter value will be executed every time a request is required (unless the parameter is ignored).
url: string, the URL specified in the behavior.
isArray: boolean, if true, then the object returned by this behavior is an array.
transformRequest: an array of functions/functions. A conversion function or an array containing conversion functions. The conversion function takes the http request body and request header and returns their conversion version (usually serialized).
transformResponse: an array of functions/functions. A conversion function or an array containing conversion functions. The conversion function takes the http response body and response header and returns their conversion version (usually serialized).
cache: boolean, if true, a default $http cache will be used as the requested cache, otherwise if there is a cache instance created with $cacheFactory, it will be used for the cache.
timeout: value, milliseconds, timeout will abort the request.
withCredentials: boolean, whether to set the XHR object withcredentials flag. See more information for credentials.
responseType: string, response header type.
interceptor: object, there are two optional methods for intercepting objects - response and responseError.
Options:Extend custom settings for $resourceProvider behavior. The only supported option is stripTrailingSlashes, boolean type. If true, the slashes at the tail of the url will be removed (default true).
Five default behaviors:
{ “get”:{method:“get”}, “save”:{method:“post”} “query”:{method:“get”,isArray:true} “remove”:{method:“delete”} “delete”:{method:“delete”} } get([params],[success],[error]); save([params],postData,[success],[error]); query([params],[success],[error]); remove([params],postData,[success],[error]); delete([params],postData,[success],[error]); $save([params],[success],[error]); $remove([params],[success],[error]);
Use code:
(function () { ("Demo", ["ngResource"]) .controller("testCtrl", ["$resource",testCtrl]); function testCtrl($resource) { var myResource = $resource("/url/_url", {}, { myPost: { method: "post", url: "/newUrl/_newUrl", params: { id: "4" }, interceptor: { response: function (d) { (d); }, responseError: function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results } } } }); ({ id: "1" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); ({ content: "text" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); ({ text: "Hello World" }, { text: "Hello World" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); ({ text: "Hello World" }, { text: "Hello World" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); ({ text: "Hello World" }, { text: "Hello World" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); var newResource = new myResource(); newResource.$save({ id: "2" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); newResource.$remove({ id: "3" }, function (d) { (d); }, function (d) { (d);//The address here is a randomly written address, so the function in error is executed, and you can print to see the parameters and results }); (); }; }());
Regarding $resource, here is just a brief introduction and use. This beast will not have a deep understanding of $resource (restful is rarely used). I hope someone can communicate related issues.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.