1
- include ../_util-fns
1
+ block includes
2
+ include ../_util-fns
2
3
3
4
:marked
4
5
One of the defining features of a single page application is its manipulation
@@ -9,12 +10,14 @@ include ../_util-fns
9
10
10
11
In this chapter we will
11
12
- [learn what structural directives are](#definition)
12
- - [study *ngIf*](#ng-if )
13
+ - [study *ngIf*](#ngIf )
13
14
- [discover the <template> element](#template)
14
15
- [understand the asterisk (\*) in **ngFor*](#asterisk)
15
16
- [write our own structural directive](#unless)
16
17
17
- [Live example](/resources/live-examples/structural-directives/ts/plnkr.html)
18
+ block liveExample
19
+ :marked
20
+ [Live example](/resources/live-examples/structural-directives/ts/plnkr.html)
18
21
19
22
<a id =" definition" ></a >
20
23
.l-main-section
@@ -42,7 +45,7 @@ include ../_util-fns
42
45
+ makeExample('structural-directives/ts/app/structural-directives.component.html' , 'structural-directives' )( format ="." )
43
46
44
47
45
- <a id =" ng-if " ></a >
48
+ <a id =" ngIf " ></a >
46
49
.l-main-section
47
50
:marked
48
51
## NgIf Case Study
@@ -163,7 +166,7 @@ figure.image-display
163
166
We can confirm these effects by wrapping the middle "hip" of the phrase "Hip! Hip! Hooray!" within a `<template>` tag.
164
167
+ makeExample('structural-directives/ts/app/structural-directives.component.html' , 'template-tag' )( format ="." )
165
168
:marked
166
- The display is a 'Hip!' short of perfect enthusiasm. The DOM effects are different when Angular is control.
169
+ The display is a 'Hip! Hooray!', short of perfect enthusiasm. The DOM effects are different when Angular is in control.
167
170
figure.image-display
168
171
img( src ='/resources/images/devguide/structural-directives/template-in-out-of-a2.png' alt ="template outside angular" )
169
172
@@ -225,18 +228,19 @@ figure.image-display
225
228
Unlike `ngIf` which displays the template content when `true`,
226
229
our directive displays the content when the condition is ***false***.
227
230
228
- :marked
229
- Creating a directive is similar to creating a component.
230
- * import the `Directive` decorator.
231
+ block unless-intro
232
+ :marked
233
+ Creating a directive is similar to creating a component.
234
+ * import the `Directive` decorator.
231
235
232
- * add a CSS **attribute selector** (in brackets) that identifies our directive.
236
+ * add a CSS **attribute selector** (in brackets) that identifies our directive.
233
237
234
- * specify the name of the public `input` property for binding
235
- (typically the name of the directive itself).
238
+ * specify the name of the public `input` property for binding
239
+ (typically the name of the directive itself).
236
240
237
- * apply the decorator to our implementation class.
241
+ * apply the decorator to our implementation class.
238
242
239
- Here is how we begin:
243
+ Here is how we begin:
240
244
241
245
+ makeExample('structural-directives/ts/app/unless.directive.ts' , 'unless-declaration' , 'unless.directive.ts (excerpt)' )( format ="." )
242
246
.l-sub-section
@@ -264,11 +268,10 @@ figure.image-display
264
268
+ makeExample('structural-directives/ts/app/unless.directive.ts' , 'unless-constructor' )( format ="." )
265
269
266
270
:marked
267
- The consumer of our directive will bind a `true` | `false` value to our directive's `myUnless` input property.
271
+ The consumer of our directive will bind a boolean value to our directive's `myUnless` input property.
268
272
The directive adds or removes the template based on that value.
269
273
270
- Let's add the `myUnless` property now as a setter-only
271
- [definedProperty](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
274
+ Let's add the `myUnless` property now as a setter-only property.
272
275
273
276
+ makeExample('structural-directives/ts/app/unless.directive.ts' , 'unless-set' )( format ="." )
274
277
.l-sub-section
@@ -278,7 +281,7 @@ figure.image-display
278
281
Nothing fancy here: if the condition is false,
279
282
we render the template, otherwise we clear the element content.
280
283
281
- The end result should look like below :
284
+ The end result should look like this :
282
285
283
286
+ makeExample('structural-directives/ts/app/unless.directive.ts' , null , 'unless.directive.ts' )
284
287
@@ -297,7 +300,7 @@ figure.image-display
297
300
Our `myUnless` directive is dead simple. Surely we left something out.
298
301
Surely `ngIf` is more complex?
299
302
300
- [Look at the source code](https://github.com/angular/angular/blob/master/modules/angular2/src/ common/directives/ng_if.ts).
303
+ [Look at the source code](https://github.com/angular/angular/blob/master/modules/%40angular/ common/src /directives/ng_if.ts).
301
304
It's well documented and we shouldn't be shy
302
305
about consulting the source when we want to know how something works.
303
306
0 commit comments