40

Is it possible to create multiple module in an Angular Script? I went through the documentation and learned that it is some kind of main method concept.

I need examples to demonstrate this.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Habibur Rahman
  • 617
  • 3
  • 7
  • 11

5 Answers5

47

Yes, you can define multiple modules in angularJS as given below.

var myApp = angular.module('myApp', [])
var myApp2 = angular.module('myApp2', [])

However, don't forget to define the dependency during each module declarations as below. Let's assume myApp2 dependent on myApp. Hence, the declaration would be something similar to,

var myApp = angular.module('myApp', [])
var myApp2 = angular.module('myApp2', ['myApp'])

The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application. However, we need to keep in mind that, we should modularize the components based on their functionality not by their types.

Aviro
  • 2,125
  • 22
  • 28
  • 1
    can we define ng-view seperately for both module... Is it possible... Thanks in advance – Akshay Kumar May 02 '16 at 17:29
  • 1
    There's only one `ng-view` supported for a single page. However, if you want to define and use multiple views / templates in a page, better to use `angular ui.router`. Here's the official documentation to that - https://github.com/angular-ui/ui-router. – Aviro May 04 '16 at 02:00
28

Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other. -- http://docs.angularjs.org/api/ng.directive:ngApp See also

https://groups.google.com/d/msg/angular/lhbrIG5aBX4/4hYnzq2eGZwJ http://docs.angularjs.org/api/angular.bootstrap

you can also use only one ng-app but combine 2 modules like this way:

var moduleA = angular.module("MyModuleA", []);
moduleA.controller("MyControllerA", function($scope) {
  $scope.name = "Bob A";
});

var moduleB = angular.module("MyModuleB", []);
moduleB.controller("MyControllerB", function($scope) {
  $scope.name = "Steve B";
});

angular.module("CombineModule", ["MyModuleA", "MyModuleB"]);

and then ng-app="CombineModule"

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
Mohamed NAOUALI
  • 2,092
  • 1
  • 23
  • 29
13

Of course you can. Just use angular.module('moduleName', [/* dependencies */]) as many times as the # of your modules you wish to create.

To get a reference to a previously defined module just do: var myModule = angular.module('moduleName'); and then myModule.controller(...), myModule.config(), myModule.constant() etc.

A suggested project layout (see Angular Seed) has a module for your app, then another for your controllers, another for your services, another for your filters and yet another for your directives. Of course this is a mere suggestion. Others suggest alternative layouts.

Thalis K.
  • 7,363
  • 6
  • 39
  • 54
13

I think that he has confused module with ngApp, in that there is only one application (ng-app) on a page and that the app's view (ngView or <ng-view>) can only exist once as well. But as stated in previous answers you can create as many modules as you like and inject them as dependencies into your "main" application module. I usually do this to separate my directives, filters, controllers and such in their own modules.

Edit:

ngApp - http://code.angularjs.org/1.1.5/docs/api/ng.directive:ngApp

m.e.conroy
  • 3,508
  • 25
  • 27
1

what do you mean multiple module? you can inject module into another module

angular.module(name[ anotherModule]);
LauroSkr
  • 2,159
  • 2
  • 15
  • 15