7
7
8
8
9
9
In this step of the tutorial, you will become familiar with the most important source code files of
10
- the AngularJS phonecat app . You will also learn how to start the development servers bundled with
11
- angular-seed, and run the application in the browser.
10
+ the AngularJS Phonecat App . You will also learn how to start the development servers bundled with
11
+ [ angular-seed][angular-seed] , and run the application in the browser.
12
12
13
- Before you continue, make sure you have set up your development environment and installed all necessary
14
- dependencies, as described in {@link index#get-started Get Started}.
13
+ Before you continue, make sure you have set up your development environment and installed all
14
+ necessary dependencies, as described in the {@link tutorial/#environment-setup Environment Setup}
15
+ section.
15
16
16
17
In the `angular-phonecat` directory, run this command:
17
18
18
19
```
19
20
git checkout -f step-0
20
21
```
21
22
22
-
23
23
This resets your workspace to step 0 of the tutorial app.
24
24
25
25
You must repeat this for every future step in the tutorial and change the number to the number of
26
26
the step you are on. This will cause any changes you made within your working directory to be lost.
27
27
28
- If you haven't already done so you need to install the dependencies by running:
28
+ If you haven't already done so, you need to install the dependencies by running:
29
29
30
30
```
31
31
npm install
32
32
```
33
33
34
- To see the app running in a browser, open a *separate* terminal/command line tab or window, then
35
- run `npm start` to start the web server. Now, open a browser window for the app and navigate to
36
- <a href=" http://localhost:8000/app/" target="_blank" title="Open app on localhost">`http://localhost:8000/app/`</a>
34
+ To see the app running in a browser, open a _separate_ terminal/command line tab or window, then run
35
+ `npm start` to start the web server. Now, open a browser window for the app and navigate to
36
+ http://localhost:8000/index.html.
37
37
38
- Note that if you already ran the master branch app prior to checking out step-0, you may see the cached
39
- master version of the app in your browser window at this point. Just hit refresh to re-load the page.
38
+ Note that if you already ran the master branch app prior to checking out step-0, you may see the
39
+ cached master version of the app in your browser window at this point. Just hit refresh to re-load
40
+ the page.
40
41
41
42
You can now see the page in your browser. It's not very exciting, but that's OK.
42
43
43
44
The HTML page that displays "Nothing here yet!" was constructed with the HTML code shown below.
44
45
The code contains some key Angular elements that we will need as we progress.
45
46
46
- __ `app/index.html`:__
47
+ ** `app/index.html`:**
47
48
48
49
```html
49
50
<!doctype html>
50
51
<html lang="en" ng-app>
51
- <head>
52
- <meta charset="utf-8">
53
- <title>My HTML File</title>
54
- <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
55
- <link rel="stylesheet" href="css/app.css">
56
- <script src="bower_components/angular/angular.js"></script>
57
- </head>
58
- <body>
59
-
60
- <p>Nothing here {{'yet' + '!'}}</p>
61
-
62
- </body>
52
+ <head>
53
+ <meta charset="utf-8">
54
+ <title>My HTML File</title>
55
+ <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
56
+ <script src="bower_components/angular/angular.js"></script>
57
+ </head>
58
+ <body>
59
+
60
+ <p>Nothing here {{'yet' + '!'}}</p>
61
+
62
+ </body>
63
63
</html>
64
64
```
65
65
66
66
67
-
68
67
## What is the code doing?
69
68
70
- **`ng-app` directive:**
69
+ <br />
70
+ **`ng-app` attribute:**
71
71
72
- <html ng-app>
72
+ ```html
73
+ <html ng-app>
74
+ ```
75
+
76
+ The `ng-app` attribute represents an Angular directive, named `ngApp` (Angular uses `kebab-case` for
77
+ its custom attributes and `camelCase` for the corresponding directives which implement them). This
78
+ directive is used to flag the HTML element that Angular should consider to be the root element of
79
+ our application. This gives application developers the freedom to tell Angular if the entire HTML
80
+ page or only a portion of it should be treated as the AngularJS application.
73
81
74
- The `ng-app` attribute represents an Angular directive named `ngApp` (Angular uses
75
- `spinal-case` for its custom attributes and `camelCase` for the corresponding directives
76
- which implement them).
77
- This directive is used to flag the html element that Angular should consider to be the root element
78
- of our application.
79
- This gives application developers the freedom to tell Angular if the entire html page or only a
80
- portion of it should be treated as the Angular application.
82
+ For more info on `ngApp`, check out the {@link ngApp API Reference}.
81
83
82
- **AngularJS script tag:**
84
+ <br />
85
+ **`angular.js` script tag:**
83
86
84
- <script src="bower_components/angular/angular.js">
87
+ ```html
88
+ <script src="bower_components/angular/angular.js"></script>
89
+ ```
85
90
86
- This code downloads the `angular.js` script which registers a callback that will be executed by the
91
+ This code downloads the `angular.js` script which registers a callback that will be executed by the
87
92
browser when the containing HTML page is fully downloaded. When the callback is executed, Angular
88
- looks for the {@link ng.directive: ngApp ngApp} directive. If
89
- Angular finds the directive, it will bootstrap the application with the root of the application DOM
90
- being the element on which the `ngApp` directive was defined.
93
+ looks for the {@link ngApp ngApp} directive. If Angular finds the directive, it will bootstrap the
94
+ application with the root of the application DOM being the element on which the `ngApp` directive
95
+ was defined.
91
96
97
+ For more info on bootstrapping your app, checkout the [Bootstrap](guide/bootstrap) section of the
98
+ Developer Guide.
99
+
100
+ <br />
92
101
**Double-curly binding with an expression:**
93
102
94
- Nothing here {{'yet' + '!'}}
103
+ ```html
104
+ Nothing here {{'yet' + '!'}}
105
+ ```
95
106
96
107
This line demonstrates two core features of Angular's templating capabilities:
97
108
98
- * a binding, denoted by double-curlies `{{ }}`
99
- * a simple expression `'yet' + '!'` used in this binding.
109
+ * A binding, denoted by double-curlies: `{{ }}`
110
+ * A simple expression used in this binding: `'yet' + '!'`
100
111
101
- The binding tells Angular that it should evaluate an expression and insert the result into the
102
- DOM in place of the binding. Rather than a one-time insert, as we'll see in the next steps, a
103
- binding will result in efficient continuous updates whenever the result of the expression
104
- evaluation changes.
112
+ The binding tells Angular that it should evaluate an expression and insert the result into the DOM
113
+ in place of the binding. As we will see in the next steps, rather than a one-time insert, a binding
114
+ will result in efficient continuous updates whenever the result of the expression evaluation
115
+ changes.
105
116
106
- {@link guide/expression Angular expression} is a JavaScript-like code snippet that is
107
- evaluated by Angular in the context of the current model scope, rather than within the scope of
108
- the global context (`window`).
117
+ {@link guide/expression Angular expressions} are JavaScript-like code snippets that are evaluated by
118
+ Angular in the context of the current model scope, rather than within the scope of the global
119
+ context (`window`).
109
120
110
- As expected, once this template is processed by Angular, the html page contains the text:
111
- "Nothing here yet!".
121
+ As expected, once this template is processed by Angular, the HTML page contains the text:
112
122
113
- ## Bootstrapping AngularJS apps
123
+ ```
124
+ Nothing here yet!
125
+ ```
114
126
115
- Bootstrapping AngularJS apps automatically using the `ngApp` directive is very easy and suitable
116
- for most cases. In advanced cases, such as when using script loaders, you can use the
117
- {@link guide/bootstrap imperative / manual way} to bootstrap the app.
127
+ ## Bootstrapping Angular Applications
118
128
119
- There are 3 important things that happen during the app bootstrap:
129
+ Bootstrapping Angular applications automatically using the `ngApp` directive is very easy and
130
+ suitable for most cases. In advanced cases, such as when using script loaders, you can use the
131
+ {@link guide/bootstrap#manual-initialization imperative/manual way} to bootstrap the application.
132
+
133
+ There are 3 important things that happen during the bootstrap phase:
120
134
121
135
1. The {@link auto.$injector injector} that will be used for dependency injection is created.
122
136
123
- 2. The injector will then create the {@link ng.$rootScope root scope} that will
124
- become the context for the model of our application.
137
+ 2. The injector will then create the {@link ng.$rootScope root scope} that will become the context
138
+ for the model of our application.
125
139
126
140
3. Angular will then "compile" the DOM starting at the `ngApp` root element, processing any
127
141
directives and bindings found along the way.
128
142
129
-
130
143
Once an application is bootstrapped, it will then wait for incoming browser events (such as mouse
131
- click , key press or incoming HTTP response ) that might change the model. Once such an event occurs,
132
- Angular detects if it caused any model changes and if changes are found, Angular will reflect them
133
- in the view by updating all of the affected bindings.
144
+ clicks , key presses or incoming HTTP responses ) that might change the model. Once such an event
145
+ occurs, Angular detects if it caused any model changes and if changes are found, Angular will
146
+ reflect them in the view by updating all of the affected bindings.
134
147
135
148
The structure of our application is currently very simple. The template contains just one directive
136
149
and one static binding, and our model is empty. That will soon change!
@@ -140,27 +153,29 @@ and one static binding, and our model is empty. That will soon change!
140
153
141
154
## What are all these files in my working directory?
142
155
143
-
144
- Most of the files in your working directory come from the [angular-seed project][angular-seed] which
145
- is typically used to bootstrap new Angular projects. The seed project is pre-configured to install
146
- the angular framework (via `bower` into the `app/bower_components/` folder) and tools for developing
147
- a typical web app (via `npm`).
156
+ Most of the files in your working directory come from the [angular-seed project][angular-seed],
157
+ which is typically used to bootstrap new AngularJS projects. The seed project is pre-configured to
158
+ install the AngularJS framework (via `bower` into the `app/bower_components/` directory) and tools
159
+ for developing and testing a typical web application (via `npm`).
148
160
149
161
For the purposes of this tutorial, we modified the angular-seed with the following changes:
150
162
151
- * Removed the example app
152
- * Added phone images to `app/img/phones/`
153
- * Added phone data files (JSON) to `app/phones/`
163
+ * Removed the example app.
164
+ * Removed unused dependencies.
165
+ * Added phone images to `app/img/phones/`.
166
+ * Added phone data files (JSON) to `app/phones/`.
154
167
* Added a dependency on [Bootstrap](http://getbootstrap.com) in the `bower.json` file.
155
168
156
169
157
-
158
170
# Experiments
159
171
160
- * Try adding a new expression to the `index.html` that will do some math:
172
+ <div></div>
161
173
162
- <p>1 + 2 = {{ 1 + 2 }}</p>
174
+ * Try adding a new expression to `index.html` that will do some math:
163
175
176
+ ```html
177
+ <p>1 + 2 = {{1 + 2}}</p>
178
+ ```
164
179
165
180
166
181
# Summary
0 commit comments