{"id":72327,"date":"2020-01-13T17:24:42","date_gmt":"2020-01-13T11:54:42","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=72327"},"modified":"2024-04-12T14:22:51","modified_gmt":"2024-04-12T08:52:51","slug":"angularjs-modules","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/","title":{"rendered":"AngularJS Modules"},"content":{"rendered":"<p>An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module.<\/p>\n<h4>Creating a Module<\/h4>\n<p>A module is created by using the AngularJS function angular.module<\/p>\n<p>&lt;div ng-app=&#8221;myApp&#8221;&gt;&#8230;&lt;\/div&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>var app = angular.module(&#8220;myApp&#8221;, []);<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>The &#8220;myApp&#8221; parameter refers to an HTML element in which the application will run. Now you can add controllers, directives, filters, and more, to your AngularJS application.<\/p>\n<h4>Adding a Controller<\/h4>\n<p>Add a controller to your application, and refer to the controller with the ng-controller directive:<\/p>\n<p>Example<\/p>\n<p>&lt;div ng-app=&#8221;myApp&#8221; ng-controller=&#8221;myCtrl&#8221;&gt;<\/p>\n<p>{{ firstName + &#8221; &#8221; + lastName }}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>var app = angular.module(&#8220;myApp&#8221;, []);<\/p>\n<p>app.controller(&#8220;myCtrl&#8221;, function($scope) {<\/p>\n<p>$scope.firstName = &#8220;Demo&#8221;;<\/p>\n<p>$scope.lastName = &#8220;User&#8221;;<\/p>\n<p>});<\/p>\n<p>&lt;\/script&gt;<\/p>\n<h4>Adding a Directive<\/h4>\n<p>AngularJS has a set of built-in directives which you can use to add functionality to your application.\u00a0 In addition you can use the module to add your own directives to your applications:<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&lt;div ng-app=&#8221;myApp&#8221; w3-test-directive&gt;&lt;\/div&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>var app = angular.module(&#8220;myApp&#8221;, []);<\/p>\n<p>app.directive(&#8220;w3TestDirective&#8221;, function() {<\/p>\n<p>return {<\/p>\n<p>template : &#8220;I was made in a directive constructor!&#8221;<\/p>\n<p>};<\/p>\n<p>});<\/p>\n<p>&lt;\/script&gt;<\/p>\n<h4>Modules and Controllers in Files<\/h4>\n<p>It is common in AngularJS applications to put the module and the controllers in JavaScript files. In this example, &#8220;myApp.js&#8221; contains an application module definition, while &#8220;myCtrl.js&#8221; contains the controller:<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;script src=&#8221;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.6.9\/angular.min.js&#8221;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;div ng-app=&#8221;myApp&#8221; ng-controller=&#8221;myCtrl&#8221;&gt;<\/p>\n<p>{{ firstName + &#8221; &#8221; + lastName }}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;script src=&#8221;myApp.js&#8221;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;script src=&#8221;myCtrl.js&#8221;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><u>myApp.js<\/u><\/p>\n<p>var app = angular.module(&#8220;myApp&#8221;, []);<\/p>\n<p>The [] parameter in the module definition can be used to define dependent modules.<\/p>\n<p>Without the [] parameter, you are not creating a new module, but retrieving an existing one.<\/p>\n<p>myCtrl.js<\/p>\n<p>app.controller(&#8220;myCtrl&#8221;, function($scope) {<\/p>\n<p>$scope.firstName = &#8220;Demo&#8221;;<\/p>\n<p>$scope.lastName= &#8220;User&#8221;;<\/p>\n<p>});<\/p>\n<h4>Functions can Pollute the Global Namespace<\/h4>\n<p>Global functions should be avoided in JavaScript. They can easily be overwritten or destroyed by other scripts. AngularJS modules reduces this problem, by keeping all functions local to the module.<\/p>\n<h4>When to Load the Library<\/h4>\n<p>While it is common in HTML applications to place scripts at the end of the &lt;body&gt; element, it is recommended that you load the AngularJS library either in the &lt;head&gt; or at the start of the &lt;body&gt;. This is because calls to angular.module can only be compiled after the library has been loaded.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;script src=&#8221;https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.6.9\/angular.min.js&#8221;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;div ng-app=&#8221;myApp&#8221; ng-controller=&#8221;myCtrl&#8221;&gt;<\/p>\n<p>{{ firstName + &#8221; &#8221; + lastName }}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>var app = angular.module(&#8220;myApp&#8221;, []);<\/p>\n<p>app.controller(&#8220;myCtrl&#8221;, function($scope) {<\/p>\n<p>$scope.firstName = &#8220;Demo&#8221;;<\/p>\n<p>$scope.lastName = &#8220;User&#8221;;<\/p>\n<p>});<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module. Creating a Module A module is created by using the AngularJS function angular.module &lt;div ng-app=&#8221;myApp&#8221;&gt;&#8230;&lt;\/div&gt; &lt;script&gt; var app = angular.module(&#8220;myApp&#8221;, []); &lt;\/script&gt;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8417],"tags":[8425],"class_list":["post-72327","page","type-page","status-publish","hentry","category-angular-js-web-development","tag-angularjs-modules"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AngularJS Modules - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AngularJS Modules - Tutorial\" \/>\n<meta property=\"og:description\" content=\"An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module. Creating a Module A module is created by using the AngularJS function angular.module &lt;div ng-app=&#8221;myApp&#8221;&gt;&#8230;&lt;\/div&gt; &lt;script&gt; var app = angular.module(&#8220;myApp&#8221;, []); &lt;\/script&gt;...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:52:51+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/\",\"name\":\"AngularJS Modules - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-13T11:54:42+00:00\",\"dateModified\":\"2024-04-12T08:52:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AngularJS Modules\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AngularJS Modules - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/","og_locale":"en_US","og_type":"article","og_title":"AngularJS Modules - Tutorial","og_description":"An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module. Creating a Module A module is created by using the AngularJS function angular.module &lt;div ng-app=&#8221;myApp&#8221;&gt;&#8230;&lt;\/div&gt; &lt;script&gt; var app = angular.module(&#8220;myApp&#8221;, []); &lt;\/script&gt;...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:51+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/","name":"AngularJS Modules - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-13T11:54:42+00:00","dateModified":"2024-04-12T08:52:51+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angularjs-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"AngularJS Modules"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=72327"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72327\/revisions"}],"predecessor-version":[{"id":72524,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/72327\/revisions\/72524"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=72327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=72327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=72327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}