{"id":20285,"date":"2015-06-08T11:20:07","date_gmt":"2015-06-08T05:50:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=20285"},"modified":"2017-08-03T15:52:36","modified_gmt":"2017-08-03T10:22:36","slug":"register-service-by-factory-provide-and-service-function-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/register-service-by-factory-provide-and-service-function-in-angularjs\/","title":{"rendered":"Register Service by Factory, $provide and Service function in AngularJS"},"content":{"rendered":"<p>In AngularJS, there are three ways to register a service in a module.<\/p>\n<ul>\n<li type=\"circle\">Module&#8217;s Factory Function<\/li>\n<li type=\"circle\">Module&#8217;s Config via $provide<\/li>\n<li type=\"circle\">Module&#8217;s Service Function<\/li>\n<\/ul>\n<ol>\n<li>\n<h3>Via Module&#8217;s Factory Function<\/h3>\n<p>We can register a service via Angular module\u2019s &#8220;factory&#8221; function. This is commonly use to register a service.<\/p>\n<ul>\n<li><strong>Registering service in the module<\/strong>\n<p>[java]<br \/>\nvar myApp = angular.module(&#8216;myApp&#8217;, []);<br \/>\nmyApp.factory(&#8216;MyService&#8217;, function() {<br \/>\n\treturn {<br \/>\n\t\tmessage: &#8216;Test message from MyService.&#8217;<br \/>\n\t}<br \/>\n});<br \/>\n[\/java]<\/p>\n<\/li>\n<li><strong>Use service in the controller<\/strong>\n<p>[java]<br \/>\nvar TestController = function($scope, MyService) {<br \/>\n   $scope.myService = MyService;<br \/>\n};<br \/>\nmyApp.controller(&#8216;TestController&#8217;, [&#8216;$scope&#8217;, &#8216;MyService&#8217;, TestController]);<br \/>\n[\/java]<\/p>\n<\/li>\n<li><strong>Use controller in the code and retrieve data from service &#8220;MyService&#8221;<\/strong>\n<p>[html]<br \/>\n&lt;div ng-controller=&quot;TestController&quot;&gt;<br \/>\n   &lt;p&gt;{{myService.message}}&lt;\/p&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h3>Via Module&#8217;s Config via $provide<\/h3>\n<p>We can register a service via <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Angular<\/a> module\u2019s &#8220;config&#8221; function. This makes service to be configurable via $get function which is called once when service instance is created (because services by default are Singletons). This is often used in mocking service in unit tests.<\/p>\n<ul>\n<li><strong>Registering service in the module<\/strong>\n<p>[java]<br \/>\nvar myApp = angular.module(&#8216;myApp&#8217;, []);<br \/>\nmyApp.config([&#8216;$provide&#8217;, function($provide) {<br \/>\n\t$provide.factory(&#8216;MyService&#8217;, function() {<br \/>\n\t\treturn {<br \/>\n\t\t\tmessage: &#8216;Test message from MyService.&#8217;<br \/>\n\t\t}<br \/>\n\t});<br \/>\n}]);<br \/>\n[\/java]<\/p>\n<\/li>\n<li><strong>Use service in the controller<\/strong>\n<p>[java]<br \/>\nvar TestController = function($scope, MyService) {<br \/>\n   $scope.myService = MyService;<br \/>\n};<br \/>\nmyApp.controller(&#8216;TestController&#8217;, [&#8216;$scope&#8217;, &#8216;MyService&#8217;, TestController]);<br \/>\n[\/java]<\/p>\n<\/li>\n<li><strong>Use controller in the code and retrieve data from service &#8220;MyService&#8221;<\/strong>\n<p>[html]<br \/>\n&lt;div ng-controller=&quot;TestController&quot;&gt;<br \/>\n   &lt;p&gt;{{myService.message}}&lt;\/p&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h3>Registering Service by service function<\/h3>\n<p>A service function is a constructor function, it creates the object using &#8220;new&#8221; keyword. We can add properties and functions to a service object by using &#8220;this&#8221; keyword and it doesn\u2019t return any object.<\/p>\n<ul>\n<li><strong>Registering service in the Angular module<\/strong>\n<p>[java]<br \/>\nmyApp.service(&#8216;MyService&#8217;, function () {<br \/>\n        this.message = &quot;Test message from MyService.&quot;;<\/p>\n<p>        this.reverseMessage = function() {<br \/>\n            return this.message.split(&#8221;).reverse().join(&#8221;);<br \/>\n        }<br \/>\n});<br \/>\n[\/java]<\/p>\n<\/li>\n<li><strong>Use service in the controller<\/strong>\n<p>[java]<br \/>\nvar TestController = function($scope, MyService) {<br \/>\n   $scope.myService = MyService;<br \/>\n};<br \/>\nmyApp.controller(&#8216;TestController&#8217;, [&#8216;$scope&#8217;, &#8216;MyService&#8217;, TestController]);<br \/>\n[\/java]<\/p>\n<\/li>\n<li><strong>Use controller in the code and retrieve data from service &#8220;MyService&#8221;<\/strong>\n<p>[html]<br \/>\n&lt;div ng-controller=&quot;TestController&quot;&gt;<br \/>\n   &lt;h1&gt;Message: {{myService.message}}&lt;\/h1&gt;<br \/>\n   &lt;h1&gt;Reversed message: {{myService.reverseMessage()}}&lt;\/h1&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Hope this helps !!!<\/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>In AngularJS, there are three ways to register a service in a module. Module&#8217;s Factory Function Module&#8217;s Config via $provide Module&#8217;s Service Function Via Module&#8217;s Factory Function We can register a service via Angular module\u2019s &#8220;factory&#8221; function. This is commonly use to register a service. Registering service in the module [java] var myApp = angular.module(&#8216;myApp&#8217;, [&hellip;]<\/p>\n","protected":false},"author":103,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[1439,3429],"tags":[3955,955,4697],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20285"}],"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\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=20285"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/20285\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=20285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=20285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=20285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}