{"id":22267,"date":"2015-07-02T01:15:13","date_gmt":"2015-07-01T19:45:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22267"},"modified":"2017-08-03T13:19:46","modified_gmt":"2017-08-03T07:49:46","slug":"lets-know-more-about-angular-module","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/lets-know-more-about-angular-module\/","title":{"rendered":"Lets know more about angular module"},"content":{"rendered":"<p>Modularizing your application is a good practice because it makes the components reusable, configurable and testable.<\/p>\n<p>As we know that angular is on the global scope itself whereas angular.module is a global place to create, register or retrieve angular modules. In other words, \u00a0angular.module is a container for other units of your angular application like services, controllers, providers, constants etc.<\/p>\n<p>[js]<br \/>\n angular.module(name, [requires], [configFn]);<br \/>\n[\/js]<\/p>\n<p>angular.module must have a name. It can also contains dependencies enclosed in brackets ([]) as second parameter and an optional configurable function.<\/p>\n<p><strong>\u00a0NOTE:<\/strong> If dependencies brackets ([]) are specified then new module is being created. Otherwise the module is being retrieved for further configuration.<\/p>\n<p>[js]<br \/>\nangular.module(&#8216;myAppModule_1&#8217;, []).service(&#8216;service_1&#8217;,function(){});<br \/>\n[\/js]<\/p>\n<p>It creates a new module having name myAppModule_1 and service &#8216;service_1&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<p>[js]<br \/>\nangular.module(&#8216;myAppModule_1&#8217;, []).service(&#8216;service_2&#8217;,function(){});<br \/>\n[\/js]<\/p>\n<p>It overwrites module &#8216;myAppModule_1&#8217; by creating a new module having name myAppModule_1 and service &#8216;service_2&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<p>[js]<br \/>\nangular.module(&#8216;myAppModule_1&#8217;).service(&#8216;service_3&#8217;,function(){});<br \/>\n[\/js]<\/p>\n<p>It will update module &#8216;myAppModule_1&#8217; adding a new service &#8216;service_3&#8217;.<\/p>\n<p><strong>Best Practice to create an angular.module<\/strong><\/p>\n<p>[js]<br \/>\n\/\/NOTE: the immediately invoked function expression is used to exemplify different files and is not required<br \/>\n(function(){<br \/>\n\/\/using the function form of use-strict<br \/>\n&quot;use strict&quot;;<br \/>\n\/\/Pass a second parameter ONLY THIS ONE TIME because<br \/>\n\/\/Re-declaration of second parameter creates bugs which are difficult to detect<br \/>\n     angular.module(&#8216;myApp&#8217;, [&#8216;ngResource&#8217;, &#8216;ngAnimate\u2019]);<\/p>\n<p>     &#8230;<br \/>\n})();<br \/>\n[\/js]<\/p>\n<p>Angular module contains the following core types of objects and components, which can be injected into each other <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">using AngularJS dependency<\/a> injection mechanism :<\/p>\n<ul>\n<li>Value<\/li>\n<li>Constant<\/li>\n<li>Provider<\/li>\n<li>Factory and<\/li>\n<li>Service.<\/li>\n<\/ul>\n<p>We&#8217;ll discuss these later.<strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p><b>There are \u00a0two types of blocks:<\/b><\/p>\n<p><b>\u00a0 \u00a0 Config block <\/b>: This is the place where application-wide stuff is configured, which needs to be performed on module loading. Only providers and constants can be injected here.<\/p>\n<p>[js]<br \/>\n(function(){<br \/>\n\/\/ using the function form of use-strict<br \/>\n&quot;use strict&quot;;<br \/>\n\/\/ accessing the module in another file by calling angular.module without the brackets([]).<br \/>\n  angular.module(&#8216;mymod&#8217;)<br \/>\n    .config([&#8216;$provide&#8217;, function ($provide) {<\/p>\n<p>     &#8230;<br \/>\n    }])<br \/>\n})();<br \/>\n[\/js]<\/p>\n<p><b>\u00a0 \u00a0 Run blocks <\/b>: They get executed after the injector is created and you would configure further system configuration here. Only instances and constants can be injected here.<\/p>\n<p>[js]<br \/>\n(function(){<br \/>\n&quot;use strict&quot;;<br \/>\n  angular.module(&#8216;mymod&#8217;)<br \/>\n    .run([&#8216;$rootScope&#8217;, function ($rootScope) {<\/p>\n<p>        &#8230;<br \/>\n    }])<br \/>\n})();<br \/>\n[\/js]<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS\u00a0<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modularizing your application is a good practice because it makes the components reusable, configurable and testable. As we know that angular is on the global scope itself whereas angular.module is a global place to create, register or retrieve angular modules. In other words, \u00a0angular.module is a container for other units of your angular application like [&hellip;]<\/p>\n","protected":false},"author":137,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1439,3429,1],"tags":[3955,955,4697,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22267"}],"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\/137"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22267"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22267\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}