@@ -48,115 +48,119 @@ figure
48
48
## Modules
49
49
figure
50
50
img( src ="/resources/images/devguide/architecture/module.png" alt ="Component" align ="left" style ="width:240px; margin-left:-40px;margin-right:10px" )
51
- :marked
52
- Angular apps are modular and Angular has its own modularity system called _Angular modules_ or _NgModules_.
53
-
54
- _Angular modules_ are a big deal.
55
- This page introduces modules; the [Angular modules](ngmodule.html) page covers them in depth.
56
51
57
- <br class =" l-clear-both" ><br >
58
- :marked
59
- Every Angular app has at least one Angular module class, [the _root module_](appmodule.html "AppModule: the root module"),
60
- conventionally named `AppModule`.
52
+ block angular-modules
53
+ :marked
54
+ Angular apps are modular and Angular has its own modularity system called _Angular modules_ or _NgModules_.
61
55
62
- While the _root module_ may be the only module in a small application, most apps have many more
63
- _feature modules_, each a cohesive block of code dedicated to an application domain,
64
- a workflow, or a closely related set of capabilities.
56
+ _Angular modules_ are a big deal.
57
+ This page introduces modules; the [Angular modules](ngmodule.html) page covers them in depth.
65
58
66
- An Angular module, whether a _root_ or _feature_, is a class with an `@NgModule` decorator.
67
- .l-sub-section
59
+ <br class =" l-clear-both" ><br >
68
60
:marked
69
- Decorators are functions that modify JavaScript classes.
70
- Angular has many decorators that attach metadata to classes so that it knows
71
- what those classes mean and how they should work.
72
- <a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841#.x5c2ndtx0" target="_blank">
73
- Learn more</a> about decorators on the web.
74
- :marked
75
- `NgModule` is a decorator function that takes a single metadata object whose properties describe the module.
76
- The most important properties are:
77
- * `declarations` - the _view classes_ that belong to this module.
78
- Angular has three kinds of view classes: [components](#components), [directives](#directives), and [pipes](pipes.html).
61
+ Every Angular app has at least one Angular module class, [the _root module_](appmodule.html "AppModule: the root module"),
62
+ conventionally named `AppModule`.
63
+
64
+ While the _root module_ may be the only module in a small application, most apps have many more
65
+ _feature modules_, each a cohesive block of code dedicated to an application domain,
66
+ a workflow, or a closely related set of capabilities.
67
+
68
+ An Angular module, whether a _root_ or _feature_, is a class with an `@NgModule` decorator.
69
+ .l-sub-section
70
+ :marked
71
+ Decorators are functions that modify JavaScript classes.
72
+ Angular has many decorators that attach metadata to classes so that it knows
73
+ what those classes mean and how they should work.
74
+ <a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841#.x5c2ndtx0" target="_blank">
75
+ Learn more</a> about decorators on the web.
76
+ :marked
77
+ `NgModule` is a decorator function that takes a single metadata object whose properties describe the module.
78
+ The most important properties are:
79
+ * `declarations` - the _view classes_ that belong to this module.
80
+ Angular has three kinds of view classes: [components](#components), [directives](#directives), and [pipes](pipes.html).
79
81
80
- * `exports` - the subset of declarations that should be visible and usable in the component [templates](#templates) of other modules.
82
+ * `exports` - the subset of declarations that should be visible and usable in the component [templates](#templates) of other modules.
81
83
82
- * `imports` - other modules whose exported classes are needed by component templates declared in _this_ module.
84
+ * `imports` - other modules whose exported classes are needed by component templates declared in _this_ module.
83
85
84
- * `providers` - creators of [services](#services) that this module contributes to
85
- the global collection of services; they become accessible in all parts of the app.
86
+ * `providers` - creators of [services](#services) that this module contributes to
87
+ the global collection of services; they become accessible in all parts of the app.
86
88
87
- * `bootstrap` - the main application view, called the _root component_,
88
- that hosts all other app views. Only the _root module_ should set this `bootstrap` property.
89
+ * `bootstrap` - the main application view, called the _root component_,
90
+ that hosts all other app views. Only the _root module_ should set this `bootstrap` property.
89
91
90
- Here's a simple root module:
91
- + makeExample('app/mini-app.ts' , 'module' , 'app/app.module.ts' )( format ='.' )
92
+ Here's a simple root module:
93
+ + makeExample('app/mini-app.ts' , 'module' , 'app/app.module.ts' )( format ='.' )
92
94
93
- .l-sub-section
95
+ .l-sub-section
96
+ :marked
97
+ The `export` of `AppComponent` is just to show how to export; it isn't actually necessary in this example. A root module has no reason to _export_ anything because other components don't need to _import_ the root module.
94
98
:marked
95
- The `export` of `AppComponent` is just to show how to export; it isn't actually necessary in this example. A root module has no reason to _export_ anything because other components don't need to _import_ the root module.
96
- :marked
97
- Launch an application by _bootstrapping_ its root module.
98
- During development you're likely to bootstrap the `AppModule` in a `main.ts` file like this one.
99
-
100
- + makeExample('app/main.ts' , '' , 'app/main.ts' )( format ='.' )
99
+ Launch an application by _bootstrapping_ its root module.
100
+ During development you're likely to bootstrap the `AppModule` in a `main.ts` file like this one.
101
+
102
+ + makeExample('app/main.ts' , '' , 'app/main.ts' )( format ='.' )
101
103
102
- :marked
103
- ### Angular modules vs. JavaScript modules
104
+ :marked
105
+ ### Angular modules vs. JavaScript modules
104
106
105
- The Angular module — a class decorated with `@NgModule` — is a fundamental feature of Angular.
107
+ The Angular module — a class decorated with `@NgModule` — is a fundamental feature of Angular.
106
108
107
- JavaScript also has its own module system for managing collections of JavaScript objects.
108
- It's completely different and unrelated to the Angular module system.
109
-
110
- In JavaScript each _file_ is a module and all objects defined in the file belong to that module.
111
- The module declares some objects to be public by marking them with the `export` key word.
112
- Other JavaScript modules use *import statements* to access public objects from other modules.
109
+ JavaScript also has its own module system for managing collections of JavaScript objects.
110
+ It's completely different and unrelated to the Angular module system.
111
+
112
+ In JavaScript each _file_ is a module and all objects defined in the file belong to that module.
113
+ The module declares some objects to be public by marking them with the `export` key word.
114
+ Other JavaScript modules use *import statements* to access public objects from other modules.
113
115
114
- + makeExample('app/app.module.ts' , 'imports' , '' )( format ='.' )
115
- + makeExample('app/app.module.ts' , 'export' , '' )( format ='.' )
116
-
117
- .l-sub-section
116
+ + makeExample('app/app.module.ts' , 'imports' , '' )( format ='.' )
117
+ + makeExample('app/app.module.ts' , 'export' , '' )( format ='.' )
118
+
119
+ .l-sub-section
120
+ :marked
121
+ <a href="http://exploringjs.com/es6/ch_modules.html" target="_blank">Learn more about the JavaScript module system on the web.</a>
118
122
:marked
119
- <a href="http://exploringjs.com/es6/ch_modules.html" target="_blank">Learn more about the JavaScript module system on the web.</a>
120
- :marked
121
- These are two different and _complementary_ module systems. Use them both to write your apps.
123
+ These are two different and _complementary_ module systems. Use them both to write your apps.
122
124
125
+ :marked
123
126
### Angular libraries
124
127
125
128
figure
126
129
img( src ="/resources/images/devguide/architecture/library-module.png" alt ="Component" align ="left" style ="width:240px; margin-left:-40px;margin-right:10px" )
127
130
128
- :marked
129
- Angular ships as a collection of JavaScript modules. You can think of them as library modules.
130
-
131
- Each Angular library name begins with the `!{_at_angular}` prefix.
132
-
133
- You install them with the **npm** package manager and import parts of them with JavaScript `import` statements.
134
- <br class =" l-clear-both" ><br >
135
- :marked
136
- For example, import Angular's `Component` decorator from the `@angular/core` library like this:
137
- + makeExample('app/app.component.ts' , 'import' , '' )( format ='.' )
138
- :marked
139
- You also import Angular _modules_ from Angular _libraries_ using JavaScript import statements:
140
- + makeExample('app/mini-app.ts' , 'import-browser-module' , '' )( format ='.' )
141
- :marked
142
- In the example of the simple root module above, the application module needs material from within that `BrowserModule`. To access that material, add it to the `@NgModule` metadata `imports` like this.
143
- + makeExample('app/mini-app.ts' , 'ngmodule-imports' , '' )( format ='.' )
144
- :marked
145
- In this way you're using both the Angular and JavaScript module systems _together_.
131
+ block angular-libraries
132
+ :marked
133
+ Angular ships as a collection of JavaScript modules. You can think of them as library modules.
146
134
147
- It's easy to confuse the two systems because they share the common vocabulary of "imports" and "exports".
148
- Hang in there. The confusion yields to clarity with time and experience.
135
+ Each Angular library name begins with the `!{_at_angular}` prefix.
136
+
137
+ You install them with the **npm** package manager and import parts of them with JavaScript `import` statements.
138
+ <br class="l-clear-both"><br>
149
139
150
- .l-sub-section
140
+ For example, import Angular's `Component` decorator from the `@angular/core` library like this:
141
+ + makeExample('app/app.component.ts' , 'import' , '' )( format ='.' )
142
+ :marked
143
+ You also import Angular _modules_ from Angular _libraries_ using JavaScript import statements:
144
+ + makeExample('app/mini-app.ts' , 'import-browser-module' , '' )( format ='.' )
145
+ :marked
146
+ In the example of the simple root module above, the application module needs material from within that `BrowserModule`. To access that material, add it to the `@NgModule` metadata `imports` like this.
147
+ + makeExample('app/mini-app.ts' , 'ngmodule-imports' , '' )( format ='.' )
151
148
:marked
152
- Learn more from the [Angular modules](ngmodule.html) page.
149
+ In this way you're using both the Angular and JavaScript module systems _together_.
150
+
151
+ It's easy to confuse the two systems because they share the common vocabulary of "imports" and "exports".
152
+ Hang in there. The confusion yields to clarity with time and experience.
153
+
154
+ .l-sub-section
155
+ :marked
156
+ Learn more from the [Angular modules](ngmodule.html) page.
153
157
154
158
.l-hr
155
159
156
- .l-main-section
160
+ .l-main-section #components
157
161
:marked
158
- <a id="components"></a>
159
162
## Components
163
+
160
164
figure
161
165
img( src ="/resources/images/devguide/architecture/hero-component.png" alt ="Component" align ="left" style ="width:200px; margin-left:-40px;margin-right:10px" )
162
166
@@ -169,7 +173,6 @@ figure
169
173
* The list of heroes.
170
174
* The hero editor.
171
175
172
-
173
176
You define a component's application logic—what it does to support the view—inside a class.
174
177
The class interacts with the view through an API of properties and methods.
175
178
@@ -253,7 +256,7 @@ block ts-decorator
253
256
Here are a few of the possible `@Component` configuration options:
254
257
255
258
:marked
256
- - `moduleId`: sets the source of the base address (`module.id`) for module-relative URLs such as the `templateUrl`.
259
+ <ul if-docs="ts"><li> `moduleId`: sets the source of the base address (`module.id`) for module-relative URLs such as the `templateUrl`.</ul>
257
260
258
261
- `selector`: CSS selector that tells Angular to create and insert an instance of this component
259
262
where it finds a `<hero-list>` tag in *parent* HTML.
@@ -262,6 +265,9 @@ block ts-decorator
262
265
263
266
- `templateUrl`: module-relative address of this component's HTML template, shown [above](#templates).
264
267
268
+ block dart-directives
269
+
270
+ :marked
265
271
- `providers`: !{_array} of **dependency injection providers** for services that the component requires.
266
272
This is one way to tell Angular that the component's constructor requires a `HeroService`
267
273
so it can get the list of heroes to display.
@@ -305,7 +311,7 @@ figure
305
311
306
312
:marked
307
313
* The `{{hero.name}}` [*interpolation*](displaying-data.html#interpolation)
308
- displays the component's `hero.name` property value within the `<li>` tags .
314
+ displays the component's `hero.name` property value within the `<li>` element .
309
315
310
316
* The `[hero]` [*property binding*](template-syntax.html#property-binding) passes the value of `selectedHero` from
311
317
the parent `HeroListComponent` to the `hero` property of the child `HeroDetailComponent`.
@@ -349,12 +355,10 @@ figure
349
355
Angular templates are *dynamic*. When Angular renders them, it transforms the DOM
350
356
according to the instructions given by **directives**.
351
357
352
- A directive is a class with directive metadata. In !{_Lang}, apply the `@Directive` !{_decorator}
353
- to attach metadata to the class.
354
- <br class =" l-clear-both" >
355
- :marked
358
+ A directive is a class with a `@Directive` !{_decorator}.
356
359
A component is a *directive-with-a-template*;
357
360
a `@Component` !{_decorator} is actually a `@Directive` !{_decorator} extended with template-oriented features.
361
+ <br class="l-clear-both">
358
362
359
363
.l-sub-section
360
364
:marked
@@ -377,7 +381,6 @@ figure
377
381
* [`*ngIf`](displaying-data.html#ngIf) includes the `HeroDetail` component only if a selected hero exists.
378
382
379
383
block dart-bool
380
- //- N/A
381
384
382
385
:marked
383
386
**Attribute** directives alter the appearance or behavior of an existing element.
@@ -491,17 +494,19 @@ figure
491
494
In brief, you must have previously registered a **provider** of the `HeroService` with the injector.
492
495
A provider is something that can create or return a service, typically the service class itself.
493
496
494
- You can register providers in modules or in components.
495
-
496
- In general, add providers to the [root module](#module) so that
497
- the same instance of a service is available everywhere.
497
+ block registering-providers
498
+ :marked
499
+ You can register providers in modules or in components.
500
+
501
+ In general, add providers to the [root module](#module) so that
502
+ the same instance of a service is available everywhere.
498
503
499
- + makeExample ('app/app.module.ts' , 'providers' , 'app/app.module.ts (module providers)') ( format = '. ')
504
+ + makeExcerpt ('app/app.module.ts (module providers)' , 'providers ' )
500
505
501
506
:marked
502
507
Alternatively, register at a component level in the `providers` property of the `@Component` metadata:
503
508
504
- + makeExample ('app/hero-list.component.ts' , 'providers' , 'app/hero-list.component.ts (component providers)') ( format = '. ')
509
+ + makeExcerpt ('app/hero-list.component.ts (component providers)' , 'providers ' )
505
510
506
511
:marked
507
512
Registering at a component level means you get a new instance of the
0 commit comments