You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Angular initializes automatically when you load the angular script into your page that contains an element
6
-
with `ng-app` directive:
5
+
For Angular to manage the DOM for your application, it needs to compile some or all of an HTML page. Angular does this initialization automatically when you load the angular.js script into your page and insert an `ng-app` directive (attribute) into one of the page's elements. For example, we can tell Angular to initialize the entire document:
7
6
8
7
<pre>
9
8
<!doctype html>
@@ -17,30 +16,36 @@ with `ng-app` directive:
17
16
</html>
18
17
</pre>
19
18
20
-
From a high-level view, this is what happens during angular's automatic initialization process:
19
+
You can also tell Angular to manage only a portion of a page. You would want to do this if you are using some other framework to manage other parts of the page. You do this by placing the `ng-app` directive on one or more container elements in the document. For example:
21
20
22
-
1. The browser loads the page, and then runs the angular script. Angular waits for the
23
-
`DOMContentLoaded` (or 'Load') event to attempt to bootstrap.
21
+
<pre>
22
+
<div ng-app>
23
+
I can add: {{ 1+2 }}
24
+
</div>
25
+
</pre>
24
26
25
-
2. Angular looks for the `ng-app` directive. If found it then proceeds to compile the DOM element and its children.
26
-
Optionally the `ng-app` may specify a {@link api/angular.module module} to load before the compilation. For details on
27
-
how the compiler works, see {@link dev_guide.compiler Angular HTML Compiler}.
27
+
You can also ask `ng-app` to load additional {@link api/angular.module modules} containing services, directives or filers that you'll use on the page.
28
+
29
+
<pre>
30
+
<div ng-app="AwesomeModule">
31
+
...
32
+
</div>
33
+
</pre
28
34
29
35
30
-
## Initialization Options
36
+
From a high-level, here's what Angular does during the initialization process:
31
37
32
-
The reason why `ng-app` exists is because angular should not assume that the entire HTML
33
-
document should be processed just because the `angular.js` script is included. In order to compile
34
-
only a part of the document set the `ng-app` on the root element of this portion.
38
+
1. The browser loads the page, and then runs the Angular script. Angular then waits for the
39
+
`DOMContentLoaded` (or 'Load') event to attempt to initialize.
35
40
36
-
## Global Angular Object
41
+
2. Angular looks for the `ng-app` directive. If found it compilies the DOM element containing `ng-app` and its children.
37
42
38
-
The angular script creates a single global variable `angular` in the global namespace. All angular
39
-
APIs are bound to fields of this global object.
43
+
3. Angular creates a global variable `angular` and binds all Angular APIs to this object's fields.
40
44
41
45
42
46
## Related Topics
43
47
48
+
* {@link dev_guide.compiler Angular HTML Compiler}
0 commit comments