{"id":9890,"date":"2013-03-13T18:41:20","date_gmt":"2013-03-13T13:11:20","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9890"},"modified":"2017-08-03T14:03:44","modified_gmt":"2017-08-03T08:33:44","slug":"angularjs-sharing-data-between-two-controllers-through-a-service","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angularjs-sharing-data-between-two-controllers-through-a-service\/","title":{"rendered":"AngularJS: Sharing Data between two controllers through a service"},"content":{"rendered":"<p style=\"text-align: justify;\">The topic of this post is quite confusing for many developers as they face problems in\u00a0identifying\u00a0the best practice to <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">share data between controllers in AngularJS<\/a>. Some of the articles out there tell to use $rootScope but that&#8217;s not the correct approach to enable this functionality.<\/p>\n<p style=\"text-align: justify;\">A more cleaner and better way of doing so is through a shared service, lets see some code below which will make everything clear.<\/p>\n<p>[js]\/\/Angular Code<br \/>\nvar app = angular.module(&#8216;myApp&#8217;, []);<\/p>\n<p>app.factory(&#8216;MyTestData&#8217;, function() {<br \/>\n    var testData = {key: &#8216;value&#8217;,key1:&#8217;value1&#8242;};<br \/>\n    return {<br \/>\n        showData: function() {<br \/>\n            console.log(&#8216;Printing Data&#8230;..&#8217;);<br \/>\n            return testData;<br \/>\n        }<br \/>\n    };<br \/>\n});<\/p>\n<p>function First($scope, MyTestData) {<br \/>\n    console.log(&#8216;First Controller&#8230;&#8230;&#8230;..&#8217;);<br \/>\n    console.log(MyTestData.showData());<br \/>\n}<\/p>\n<p>function Second($scope, MyTestData) {<br \/>\n    console.log(&#8216;Second Controller&#8230;&#8230;&#8230;.&#8217;);<br \/>\n    console.log(MyTestData.showData());<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>&nbsp;<\/p>\n<p>[html]&lt;!&#8211;html code&#8211;&gt;&lt;\/pre&gt;<br \/>\n&lt;div&gt;&lt;\/div&gt;<br \/>\n&lt;div&gt;&lt;\/div&gt;<br \/>\n&lt;pre&gt;<br \/>\n[\/html]<\/p>\n<p style=\"text-align: justify;\">When you run the above code, it will console the value of testData in the browser from both the controllers. In the above code we have used .factory which is another way of <a title=\"Building Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">defining a service<\/a>, you can find more information regarding that in angular docs. I hope it helps, feel free to ask if you have any queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The topic of this post is quite confusing for many developers as they face problems in\u00a0identifying\u00a0the best practice to share data between controllers in AngularJS. Some of the articles out there tell to use $rootScope but that&#8217;s not the correct approach to enable this functionality. A more cleaner and better way of doing so is [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1439,3429],"tags":[3955,955,4697,1143,1144],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9890"}],"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\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9890"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9890\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}