{"id":20056,"date":"2015-05-30T12:10:58","date_gmt":"2015-05-30T06:40:58","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20056"},"modified":"2017-08-03T15:56:51","modified_gmt":"2017-08-03T10:26:51","slug":"angulars-resource-for-crud-operations","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angulars-resource-for-crud-operations\/","title":{"rendered":"Angular\u2019s $resource for CRUD Operations"},"content":{"rendered":"<p><strong>$resouce<\/strong> is a perfect option to create a single page application which involve <strong>CRUD<\/strong> operations.You don&#8217;t write your CRUD methods (create,read,update and delete) when you use it.<\/p>\n<p>A RESTful web service with one endpoint for a data type that does different things with that data type based on HTTP methods like\u00a0GET,\u00a0POST,\u00a0PUT,\u00a0DELETE, etc. So with a\u00a0$resource service, you can call a\u00a0GET method to get the resource as a JavaScript object, then alter it and send it back using POST method, or may perform even delete operation with\u00a0DELETE method.<br \/>\nAs we already said that $resource is use full for REStful service so it expects a classic RESTful back-end and your end point should be in the following format:<\/p>\n<table class=\"reference\" style=\"height: 241px;\" width=\"693;\">\n<tbody>\n<tr>\n<th>Method<\/th>\n<th>API URL<\/th>\n<th>PAYLOAD<\/th>\n<th>Result<\/th>\n<\/tr>\n<tr>\n<td>GET<\/td>\n<td><\/td>\n<td>empty<\/td>\n<td>Returns all products<\/td>\n<\/tr>\n<tr>\n<td>POST<\/td>\n<td><\/td>\n<td>JSON Object<\/td>\n<td>New product created<\/td>\n<\/tr>\n<tr>\n<td>GET<\/td>\n<td><\/td>\n<td>empty<\/td>\n<td>Returns single product<\/td>\n<\/tr>\n<tr>\n<td>PUT<\/td>\n<td><\/td>\n<td>JSON Object<\/td>\n<td>Returns all products<\/td>\n<\/tr>\n<tr>\n<td>DELETE<\/td>\n<td><\/td>\n<td>empty<\/td>\n<td>Delete existing product<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Let\u2019s use it<\/strong><\/p>\n<p>You need to download a file called\u00a0<strong>angular-resource.js<\/strong>\u00a0and include it in your HTML page<\/p>\n<p>[js]<br \/>\n&lt;script src=\u00a0&lt;strong&gt;angular-resource.js&lt;\/script&gt;<br \/>\n[\/js]<\/p>\n<p>Then add as module dependency:<\/p>\n<p>[js]<br \/>\nangular.module(&quot;myApp&quot;, [&quot;ngResource&quot;]) \/\/myApp is our main<br \/>\n[\/js]<\/p>\n<p>After this create a service\u00a0in you app with $resouce dependency.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>[js]<br \/>\n$resource(url,{paramDefaults},{actions},{options});<br \/>\n[\/js]<\/p>\n<p>Where url is required parameter which is web app url for endpoints.paramDefaults are defaults for url params. The action methods which map to CRUD operations (Note: you can have your own action methods).options are Used to remove the trailing slashes from the url with the help of stripTrailingSlashes property.<\/p>\n<p>Object of $resource returned\u00a0following action methods to perform CRUD operations<\/p>\n<p>&#8216;<strong>save<\/strong>&#8216;: {method:&#8217;POST&#8217;},<br \/>\n&#8216;<strong>get<\/strong>&#8216;: {method:&#8217;GET&#8217;},<br \/>\n&#8216;<strong>query<\/strong>&#8216;: {method:&#8217;GET&#8217;, isArray:true},<br \/>\n&#8216;<strong>delete<\/strong>&#8216;: {method:&#8217;DELETE&#8217;} };<br \/>\n&#8216;<strong>remove<\/strong>&#8216;: {method:&#8217;DELETE&#8217;},<\/p>\n<p>after this\u00a0, you can use them in your controller to perform CRUD operations:<\/p>\n<p>Example &#8211;<\/p>\n<p>Service :<\/p>\n<p>[js]<br \/>\nangular.module(&#8216;myApp&#8217;)<br \/>\n.factory(&#8216;ProductService&#8217;, [&#8216;$resource&#8217;,<br \/>\n    function ($resource) {<br \/>\n        return $resource(&#8216;\/products&#8217;, {}, {<br \/>\n            query: { method: &quot;GET&quot;, isArray: true },<br \/>\n            create: { method: &quot;POST&quot;},<br \/>\n            get: { method: &quot;GET&quot;},<br \/>\n            remove: { method: &quot;DELETE&quot;},<br \/>\n            update: { method: &quot;PUT&quot;}<br \/>\n\t\t});<\/p>\n<p>}]);<br \/>\n[\/js]<\/p>\n<p>Controller:<\/p>\n<p>[js]<br \/>\nangular.module(&#8216;myApp&#8217;)<br \/>\n.controller(&#8216;ProductCtrl&#8217;, [ &#8216;ProductService&#8217;, &#8216;$scope&#8217;, function (ProductService, $scope) {<\/p>\n<p>    \/\/ Create a new product<br \/>\n\t$scope.createProduct = function() {<br \/>\n\t\tvar payload = {<br \/>\n    \t\t\tname: &#8216;Shirt&#8217;,<br \/>\n\t\t\t    color: &#8216;red&#8217;,<br \/>\n\t\t\t    size: &#8216;small&#8217;<br \/>\n\t\t};<br \/>\n\t\tProductService.create(payload, function(data) {<br \/>\n     \t\t\t\/\/ do something which you want with response<br \/>\n\t\t});<br \/>\n\t};<\/p>\n<p>\t\/\/ Get a single product<br \/>\n\t$scope.getProduct = function() {<br \/>\n\t\tProductService.get({id: 1234}, function(data) {<br \/>\n\t\t   \/\/ do something which you want with response<br \/>\n\t\t});<br \/>\n\t};<\/p>\n<p>\t\/\/ Update a product<br \/>\n\t$scope.updateProduct = function() {<br \/>\n\t\tProductService.update({id: 1234,color:blue}, function(data) {<br \/>\n     \t\t\/\/ do something which you want with response<br \/>\n       });<br \/>\n\t};<\/p>\n<p>\t\/\/ Delete a product<br \/>\n\t$scope.deleteProduct = function() {<br \/>\n\t\tProductService.remove({id: 1234}, function(data) {<br \/>\n   \t\t \/\/ do something which you want with response<br \/>\n  \t\t});<br \/>\n\t};<\/p>\n<p>}]);<br \/>\n[\/js]<\/p>\n<p>So in this way we can quickly <a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">create a Single Page Applications which involve CRUD<\/a> operations.<\/p>\n<p>Hope this helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>$resouce is a perfect option to create a single page application which involve CRUD operations.You don&#8217;t write your CRUD methods (create,read,update and delete) when you use it. A RESTful web service with one endpoint for a data type that does different things with that data type based on HTTP methods like\u00a0GET,\u00a0POST,\u00a0PUT,\u00a0DELETE, etc. So with a\u00a0$resource [&hellip;]<\/p>\n","protected":false},"author":146,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1439,3429,1185],"tags":[1820,3955,1186,1822,4697,55,1156,1821],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20056"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/146"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20056"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20056\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}