Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ng-app not ng-module?

I understand that ng-app initializes a module in AngularJS as follows:

var app = angular.module('myApp', []);
<html ng-app="myApp">

But when I teach this to someone new to AngularJS or watch a video, instructors inevitable fumble over the inconsistency in the terminology between app and module. AngularJS is so well thought out as a framework that I'm surprised it hasn't changed to:

var app = angular.app('myApp', []);
<html ng-app="myApp">

OR

var app = angular.module('myModule', []);
<html ng-module="myModule">

Has anyone been following the project long enough to know the history on this part of the framework?

like image 352
Craig McKeachie Avatar asked Nov 12 '13 16:11

Craig McKeachie


People also ask

Why Angular application is not working?

To resolve the problem, you need to import the missing module into your module. Most of the cases, that module would be the AppModule in your app directory. Only import the modules you really need! Importing unnecessarily module bloats your application size significantly.

Is app module is mandatory in Angular?

Every application that executes in a browser needs the BrowserModule from @angular/platform-browser . So every such application includes the BrowserModule in its root AppModule 's imports array. Other guide and cookbook pages will tell you when you need to add additional modules to this array.

Why we use NG-app in Angular?

The ng-app directive defines the root element of an AngularJS application and starts an AngularJS Application. The ng-app directive will auto-bootstrap (automatically initialize) the application when a web page is loaded. It is also used to load various AngularJS modules in AngularJS Application.

Can Angular applications ng-app be nested within each other?

AngularJS applications cannot be nested within each other. Do not use a directive that uses transclusion on the same element as ngApp . This includes directives such as ngIf , ngInclude and ngView .


3 Answers

ng-app means: That page has Angular in it!

ng-app="module" means: That page has Angular in it and necessary controls/services/etc are defined in that module.

like image 30
Beterraba Avatar answered Oct 22 '22 05:10

Beterraba


I don't think Craig is asking what does ng-app do or how does it work.

I think he's asking why did the people that created angular name that directive ng-app. Why didn't they name it ng-module. ng-module would be easier to understand.

For example ng-controller should name a controller, ng-module should name a module. The angular methods to create them are named module() and controller(), there is no method or entity called "app".

I tend to agree with Craig. That said if I were go speculate why they named it ng-app I would think it's because you are only allowed to have one ng-app directive in your HTML. If you wanted to have more than one module associated with your HTML page you can do it programmatically.

So ng-app is more of a utility to bootstrap your HTML with a module, it is not a generic way to associate modules with your HTML.

If you look at the documentation that's what it suggests:

Use this directive to auto-bootstrap an AngularJS application. The ngApp directive designates the root element of the application and is typically placed near the root element of the page - e.g. on the or tags.

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

All that said if you want an ng-module directive you could always write your own to wrap the angular.bootstrap() function. You can find more details and code about how to do this on a blog post I wrote about it: AngularJS: Getting around ngApp limitations with ngModule

like image 116
Luis Perez Avatar answered Oct 22 '22 06:10

Luis Perez


ng-app defines the main or bootstrap module of your application, which should perform the initialization task of your application. There may be case where at run time you want to decide what which should be the main module of your application. Like in java you have many methods and classes but you define one main method as starting point. Similarly, in angular you have many module, however, you define one module as the starting point of application.

like image 4
Vijay Pande Avatar answered Oct 22 '22 06:10

Vijay Pande