{"id":13296,"date":"2014-05-02T13:28:30","date_gmt":"2014-05-02T07:58:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13296"},"modified":"2017-02-12T19:21:12","modified_gmt":"2017-02-12T13:51:12","slug":"common-functionality-among-multiple-controllers","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/common-functionality-among-multiple-controllers\/","title":{"rendered":"Common functionality among Multiple Controllers"},"content":{"rendered":"<p>Many a times, we see a requirement, where we have to write same <strong>functions<\/strong> in multiple <strong>controllers<\/strong> just to <strong>share<\/strong> a <strong>common<\/strong> <strong>functionality<\/strong> among them, which is a bad approach, because in future, if any changes are required to that <strong>function<\/strong>, we\u2019ll have to make the changes manually in all the copies of that <strong>function<\/strong> where it is defined or if we want to add that particular <strong>functionality<\/strong> in another <strong>controller<\/strong>, again we\u2019ll have to copy-paste the whole <strong>function<\/strong> <strong>scope<\/strong> in that <strong>controller<\/strong> too. And doing so is completely against the <strong>DRY (Don&#8217;t Repeat Yourself) principle<\/strong>.<\/p>\n<p>So to solve this problem, we write <strong>directives<\/strong>. Let\u2019s play more with the <strong>directives<\/strong> \ud83d\ude09<\/p>\n<p><strong>NOTE:<\/strong> If you are not aware with the basics of <strong>directives<\/strong>, then I will advice you to please read the links below to get to know an idea of <strong>Directives<\/strong>:<br \/>\n1. <a href=\"http:\/\/www.itsgroovybaby.in\/blog\/javascript\/angularjs\/angularjs-directives-introduction\/\" target=\"_blank\">Directive Introduction<\/a><br \/>\n2. Components in AngularJS<\/p>\n<p>In my AngularJS Routes blog, we made a single page application (With user CRUD functionality) demo. We were having remove user functionality only in LIST page, but now we want to add remove <strong>functionality<\/strong> to SHOW and EDIT Page. So instead of doing copy-paste in SHOW and EDIT page, we are going to write <strong>directive<\/strong> for that as below:<br \/>\n[js]<br \/>\n\/*<br \/>\n * Defining RemoveUserFunctionality Directive, to share it among<br \/>\n * multiple controllers. This directive injects remove function to scope where it is marked.<br \/>\n * *\/<br \/>\nrouteTestModule.directive(&#8216;removeUserFunctionality&#8217;, [&#8216;$location&#8217;, function ($location) {<br \/>\n    return {<br \/>\n        scope: true,<br \/>\n        link: function ($scope) {<br \/>\n            \/*<br \/>\n             * We remove function from ListUserController and place it here (in directive),<br \/>\n             * And inject remove function everywhere with the help of this directive.<br \/>\n             * *\/<br \/>\n            $scope.remove = function (id) {<br \/>\n                $scope.users = _.filter(users, function (user) {<br \/>\n                    return user.id != id;<br \/>\n                });<br \/>\n                users = $scope.users;<br \/>\n                $location.path(&amp;quot;\/user\/list&amp;quot;);<br \/>\n            };<br \/>\n        }<br \/>\n    }<br \/>\n}]);<br \/>\n[\/js]<br \/>\nLet\u2019s use removeUserFunctionality <strong>directive<\/strong> and add Remove function to EDIT and SHOW Pages as below:<\/p>\n<p><strong>partials\/edit.html:<\/strong><br \/>\n[html]<br \/>\n&amp;lt;!&#8211; We are using remove-user-functionality directive to inject Remove function into scope &#8211;&amp;gt;<br \/>\n&amp;lt;table remove-user-functionality&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Name&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;{{user.name}}&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Age&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;{{user.age}}&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;a ng-href=&amp;quot;#\/user\/edit\/{{user.id}}&amp;quot;&amp;gt;Edit&amp;lt;\/a&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Remove&amp;quot; ng-click=&amp;quot;remove(user.id)&amp;quot; \/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;!&#8211;<br \/>\n        Here Remove function is not defined into EditUserController, its injected by<br \/>\n        remove-user-functionality directive.<br \/>\n        &#8211;&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n&amp;lt;\/table&amp;gt;<br \/>\n[\/html]<\/p>\n<p><strong>partials\/show.html<\/strong>:<br \/>\n[html]<br \/>\n&amp;lt;!&#8211; We are using remove-user-functionality directive to inject Remove function into scope &#8211;&amp;gt;<br \/>\n&amp;lt;table remove-user-functionality&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Name&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; ng-model=&amp;quot;user.name&amp;quot; \/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Age&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; ng-model=&amp;quot;user.age&amp;quot; \/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Cancel&amp;quot; ng-click=&amp;quot;cancel()&amp;quot;\/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Update&amp;quot; ng-click=&amp;quot;update()&amp;quot;\/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Remove&amp;quot; ng-click=&amp;quot;remove(user.id)&amp;quot; \/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;!&#8211;<br \/>\n        Here also Remove function is not defined in ShowUserController, rather injected by<br \/>\n        remove-user-functionality directive.<br \/>\n        &#8211;&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n&amp;lt;\/table&amp;gt;<br \/>\n[\/html]<\/p>\n<p><strong>partials\/list.html:<\/strong><br \/>\n[html]<br \/>\n&amp;lt;!&#8211; We are using remove-user-functionality directive to inject remove function into scope &#8211;&amp;gt;<br \/>\n&amp;lt;table remove-user-functionality&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Id&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Name&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;Age&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr ng-repeat=&amp;quot;user in users&amp;quot;&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;a ng-href=&amp;quot;#\/user\/show\/{{user.id}}&amp;quot;&amp;gt;{{user.id}}&amp;lt;\/a&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;{{user.name}}&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;{{user.age}}&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;a ng-href=&amp;quot;#\/user\/edit\/{{user.id}}&amp;quot;&amp;gt;Edit&amp;lt;\/a&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Remove&amp;quot; ng-click=&amp;quot;remove(user.id)&amp;quot; \/&amp;gt;&amp;lt;\/td&amp;gt;<br \/>\n        &amp;lt;!&#8211;<br \/>\n        Again Remove function is not defined in UserListController, it is injected by<br \/>\n        remove-user-functionality directive.<br \/>\n        &#8211;&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n&amp;lt;\/table&amp;gt;<br \/>\n[\/html]<\/p>\n<p>After this, if any change is required, we have to make it only in removeUserFunctionality <strong>Directive<\/strong>, and we can use it everywhere. \ud83d\ude42<\/p>\n<p><strong>NOTE:<\/strong> You can checkout full working source code from this <a href=\"https:\/\/github.com\/AmitThakkar\/AngularDemo\" target=\"_blank\">link<\/a>.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many a times, we see a requirement, where we have to write same functions in multiple controllers just to share a common functionality among them, which is a bad approach, because in future, if any changes are required to that function, we\u2019ll have to make the changes manually in all the copies of that function [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[955,1419,1420,867,1300,456],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13296"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13296"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13296\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}