{"id":18659,"date":"2015-03-30T15:28:28","date_gmt":"2015-03-30T09:58:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18659"},"modified":"2017-08-03T15:57:45","modified_gmt":"2017-08-03T10:27:45","slug":"configurable-service-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/configurable-service-in-angularjs\/","title":{"rendered":"Configurable Service in AngularJS"},"content":{"rendered":"<p><a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Angular<\/a> provide services for handling non-view logic, communication with server(back-end) and can holds data &amp; state. These services are singleton objects that can be used to share and organize code. For doing so, angular provides us five cool ways to make a service on the bases of once need.<\/p>\n<p>Following are the five approaches that can be used to create a service in angular:<\/p>\n<p>1) Value<\/p>\n<p>[code language=&#8221;javascript&#8221;]app.value(&#8216;name&#8217;, &#8216;Vibhor Kukreja&#8217;);[\/code]<\/p>\n<p>2) Constant<\/p>\n<p>[code language=&#8221;javascript&#8221;]app.constant(&#8216;name&#8217;, &#8216;Vibhor Kukreja&#8217;);[\/code]<\/p>\n<p>3) Factory<\/p>\n<p>[code language=&#8221;javascript&#8221;]app.factory(&#8216;myService&#8217;, function(){<br \/>\n        return{<br \/>\n              getName: function() {<br \/>\n                       return &#8216;Vibhor Kukreja&#8217;;<br \/>\n                       }<br \/>\n              }<br \/>\n});[\/code]<\/p>\n<p>4) Service<\/p>\n<p>[code language=&#8221;javascript&#8221;]app.service(&#8216;myService&#8217;, function() {<br \/>\n       var name = &#8216;Vibhor Kukreja&#8217;;<br \/>\n       this.myName = function() {<br \/>\n                      return name;<br \/>\n                    }<br \/>\n});[\/code]<\/p>\n<p>5) Provider<\/p>\n<p>[code language=&#8221;javascript&#8221;]app.config(function($provide){<br \/>\n\t\t$provide.provider(&#8216;myService&#8217;, function(){<br \/>\n\t\t\treturn {<br \/>\n\t\t\t\t$get: function() {<br \/>\n\t\t\t\t\treturn {<br \/>\n\t\t\t\t\t\tname: &#8216;Vibhor Kukreja&#8217;<br \/>\n\t\t\t\t\t\t}<br \/>\n\t\t\t\t\t}<br \/>\n\t\t\t\t}<br \/>\n\t\t});<br \/>\n});[\/code]<\/p>\n<p>All these approaches have their own key use cases. Depending upon the use case, one can pick an approach to implement a service.<\/p>\n<p>The main focus of the blog is on the Provider approach of Angular Services.<\/p>\n<p>The provider approach allows us to make a service from a much lower level, which makes the service to be configurable. In this, we have a function that returns an object with a function named $get.<br \/>\nThe angular calls this $get function once to create your service(since it is singleton) at the start application<\/p>\n<p>Now, we can configure the service with the provider approach before it gets created.<\/p>\n<p>[code language=&#8221;javascript&#8221;]app.config(function($provide){<br \/>\n\t$provide.provider(&#8216;myService&#8217;, function(){<br \/>\n\t\tvar msg;<br \/>\n\t\treturn {<br \/>\n\t\t\tmyConfig: function(greet) {<br \/>\n\t\t\t\t\tmsg = greet;<br \/>\n\t\t\t},<br \/>\n\t\t\t$get: function() {<br \/>\n\t\t\t\treturn {<br \/>\n\t\t\t\t\tname: msg + &#8216; Vibhor Kukreja&#8217;<br \/>\n\t\t\t\t\t}<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t});<br \/>\n});<\/p>\n<p>app.config(function (myServiceProvider)) {<br \/>\n\tmyServiceProvider.myConfig(&#8216;Hello&#8217;);<br \/>\n}[\/code]<\/p>\n<p>The thing that we need to remember is that, the $get function is only called once, at the time of <a title=\"Building Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">application configuration phase<\/a>. And once you have used the service on run-time then you can&#8217;t re-configure it.<\/p>\n<p>Hope this will help.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Angular provide services for handling non-view logic, communication with server(back-end) and can holds data &amp; state. These services are singleton objects that can be used to share and organize code. For doing so, angular provides us five cool ways to make a service on the bases of once need. Following are the five approaches that [&hellip;]<\/p>\n","protected":false},"author":99,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1185],"tags":[3955,955,4697,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18659"}],"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\/99"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18659"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18659\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}