|
2 | 2 | @name Developer Guide: Initializing Angular: Automatic Initialization |
3 | 3 | @description |
4 | 4 |
|
5 | | -Angular initializes automatically when you load the angular script into your page, specifying |
6 | | -angular's `ng:autobind` attribute with no arguments: |
7 | | - |
8 | | - <script src="angular.js" ng:autobind> |
| 5 | +Angular initializes automatically when you load the angular script into your page that contains an element |
| 6 | +with `ng:app` directive: |
| 7 | + |
| 8 | + <html ng:app> |
| 9 | + <head> |
| 10 | + <script src="angular.js"></script> |
| 11 | + </head> |
| 12 | + <body> |
| 13 | + I can add: {{ 1+2 }}. |
| 14 | + </body> |
| 15 | + </html> |
9 | 16 |
|
10 | 17 | From a high-level view, this is what happens during angular's automatic initialization process: |
11 | 18 |
|
12 | | -1. The browser loads the page, and then runs the angular script. |
13 | | - |
14 | | - The `ng:autobind` attribute tells angular to compile and manage the whole HTML document. The |
15 | | -compilation phase is initiated in the page's `onLoad()` handler. Angular doesn't begin processing |
16 | | -the page until after the page load is complete. |
17 | | - |
18 | | -2. Angular finds the root of the HTML document and creates the global variable `angular` in the |
19 | | -global namespace. Everything that angular subsequently creates is bound to fields in this global |
20 | | -object. |
21 | | - |
22 | | -3. Angular walks the DOM looking for angular widgets, directives, and markup (such as `ng:init` or |
23 | | -`ng:repeat`). As angular encounters these, it creates child scopes as necessary and attaches them |
24 | | -to the DOM, registers listeners on those scopes, associates any controller functions with their |
25 | | -data and their part of the view, and ultimately constructs a runnable application. The resulting |
26 | | -app features two-way data-binding and a nice separation between data, presentation, and business |
27 | | -logic. |
| 19 | +1. The browser loads the page, and then runs the angular script. Angular waits for the |
| 20 | +`DOMContentLoaded` (or 'Load') event to attempt to bootstrap. |
28 | 21 |
|
29 | | -4. For the duration of the application session (while the page is loaded), angular monitors the |
30 | | -state of the application, and updates the view and the data model whenever the state of either one |
31 | | -changes. |
32 | | - |
33 | | -For details on how the compiler works, see {@link dev_guide.compiler Angular HTML Compiler}. |
| 22 | +2. Angular looks for the `ng:app` directive. If found it then proceeds to compile the DOM element and its children. |
| 23 | +Optionally the `ng:app` may specify a {@link api/angular.module module} to load before the compilation. For details on |
| 24 | +how the compiler works, see {@link dev_guide.compiler Angular HTML Compiler}. |
34 | 25 |
|
35 | 26 |
|
36 | 27 | ## Initialization Options |
37 | 28 |
|
38 | | -The reason why `ng:autobind` exists is because angular should not assume that the entire HTML |
| 29 | +The reason why `ng:app` exists is because angular should not assume that the entire HTML |
39 | 30 | document should be processed just because the `angular.js` script is included. In order to compile |
40 | | -only a part of the document, specify the ID of the element you want to use for angular's root |
41 | | -element as the value of the `ng:autobind` attribute: |
42 | | - |
43 | | - ng:autobind="angularContent" |
44 | | - |
45 | | - |
46 | | -## Auto-bootstrap with `#autobind` |
47 | | - |
48 | | -In some rare cases you can't define the `ng:` prefix before the script tag's attribute (for |
49 | | -example, in some CMS systems). In those situations it is possible to auto-bootstrap angular by |
50 | | -appending `#autobind` to the `<script src=...>` URL, like in this snippet: |
51 | | - |
52 | | -<pre> |
53 | | - <!doctype html> |
54 | | - <html> |
55 | | - <head> |
56 | | - <script src="http://code.angularjs.org/angular.js#autobind"></script> |
57 | | - </head> |
58 | | - <body> |
59 | | - <div xmlns:ng="http://angularjs.org"> |
60 | | - Hello {{'world'}}! |
61 | | - </div> |
62 | | - </body> |
63 | | - </html> |
64 | | -</pre> |
65 | | - |
66 | | -As with `ng:autobind`, you can specify an element id that should be exclusively targeted for |
67 | | -compilation as the value of the `#autobind`, for example: `#autobind=angularContent`. |
68 | | - |
69 | | -If angular.js file is being combined with other scripts into a single script file, then all of the |
70 | | -config options above apply to this processed script as well. That means if the contents of |
71 | | -`angular.js` were appended to `all-my-scripts.js`, then the app can be bootstrapped as: |
72 | | - |
73 | | -<pre> |
74 | | - <!doctype html> |
75 | | - <html xmlns:ng="http://angularjs.org"> |
76 | | - <head> |
77 | | - <script src="http://myapp.com/all-my-scripts.js" ng:autobind></script> |
78 | | - </head> |
79 | | - <body> |
80 | | - <div> |
81 | | - Hello {{'world'}}! |
82 | | - </div> |
83 | | - </body> |
84 | | - </html> |
85 | | -</pre> |
86 | | - |
| 31 | +only a part of the document set the `ng:app` on the root element of this portion. |
87 | 32 |
|
88 | 33 | ## Global Angular Object |
89 | 34 |
|
|
0 commit comments