Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 034a5a6

Browse files
committed
docs(template-syntax): Clarify input/output aliasing syntax
closes #722
1 parent fe5c605 commit 034a5a6

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

public/docs/_examples/template-syntax/ts/app/app.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ <h3>
257257
<!-- `myClick` is an event on the custom `MyClickDirective` -->
258258

259259
<!-- #docregion my-click -->
260-
<div myClick (myClick)="clickity=$event">click with myClick</div>
260+
<div myClick (myClick)="clickMessage=$event">click with myClick</div>
261261
<!-- #enddocregion my-click -->
262262
<!-- #enddocregion event-binding-3 -->
263-
{{clickity}}
263+
{{clickMessage}}
264264
</div>
265265

266266

@@ -567,8 +567,8 @@ <h4>Example Form</h4>
567567
</hero-detail>
568568
<!-- #enddocregion io-2 -->
569569

570-
<div myClick2 (myClick)="clicked2=$event">myClick2</div>
571-
{{clicked2}}
570+
<div myClick2 (myClick)="clickMessage2=$event">myClick2</div>
571+
{{clickMessage2}}
572572

573573
<!-- Pipes -->
574574
<hr><h2>Pipes</h2>

public/docs/_examples/template-syntax/ts/app/my-click.directive.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,36 @@ import {Directive, Output, ElementRef, EventEmitter} from 'angular2/core';
44
@Directive({selector:'[myClick]'})
55
export class MyClickDirective {
66
// #docregion my-click-output-1
7-
@Output('myClick') clicks = new EventEmitter<string>();
7+
@Output('myClick') clicks = new EventEmitter<string>(); // @Output(alias) propertyName = ...
88
// #enddocregion my-click-output-1
9+
910
constructor(el: ElementRef){
1011
el.nativeElement
1112
.addEventListener('click', (event:Event) => {
12-
this.clicks.emit('Click!');
13+
this._toggle = !this._toggle;
14+
this.clicks.emit(this._toggle ? 'Click!' : '');
1315
});
1416
}
17+
_toggle = false;
1518
}
1619

1720
// #docregion my-click-output-2
1821
@Directive({
1922
// #enddocregion my-click-output-2
2023
selector:'[myClick2]',
2124
// #docregion my-click-output-2
22-
outputs:['clicks:myClick']
25+
outputs:['clicks:myClick'] // propertyName:alias
2326
})
2427
// #enddocregion my-click-output-2
2528
export class MyClickDirective2 {
2629
clicks = new EventEmitter<string>();
30+
2731
constructor(el: ElementRef){
2832
el.nativeElement
2933
.addEventListener('click', (event:Event) => {
30-
this.clicks.emit('Click!');
34+
this._toggle = !this._toggle;
35+
this.clicks.emit(this._toggle ? 'Click2!' : '');
3136
});
3237
}
38+
_toggle = false;
3339
}

public/docs/ts/latest/guide/template-syntax.jade

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ include ../../../../_includes/_util-fns
101101
We write template expressions in a language that looks like JavaScript.
102102
Many JavaScript expressions are legal template expressions but not all.
103103
JavaScript expressions that have or promote side-effects are prohibited including:
104-
* assignment (`=`)
104+
* assignments (`=`, `+=`, `-=`)
105105
* the `new` operator
106106
* chaining expressions with `;` or `,`
107107
* increment and decrement operators, `++` and `--`.
@@ -191,11 +191,12 @@ include ../../../../_includes/_util-fns
191191
:marked
192192
Angular template statements are also written in a language that looks like JavaScript.
193193
The template statement parser is different than the template expression parser and
194-
specifically supports both assignment (=) and chaining expressions with semicolons (;) and commas (,).
194+
specifically supports both basic assignment (=) and chaining expressions with semicolons (;) and commas (,).
195195

196196
However, certain JavaScript syntax is not allowed:
197197
* the `new` operator
198198
* increment and decrement operators, `++` and `--`
199+
* operator assignment, e.g. `+=`, `-=`
199200
* bit-wise operators, `|` and `&`
200201
* the [template expression operators](#expression-operators)
201202

@@ -1172,28 +1173,30 @@ figure.image-display
11721173

11731174
This is frequently the case with [Attribute Directives](attribute-directives.html).
11741175
Directive consumers expect to bind to the name of the directive.
1175-
For example, we expect to bind to the `myClick` event property of the `MyClickDirective`.
1176-
1177-
The directive name is often a poor choice for the the internal property name
1178-
because it rarely describes what the property does.
1179-
`myClick` is not a good name for a property that emits click events.
1176+
For example, when we apply a directive with a `myClick` selector to a `<div>` tag,
1177+
we expect to bind to an event property that is also called `myClick`.
1178+
+makeExample('template-syntax/ts/app/app.component.html', 'my-click')(format=".")
1179+
:marked
1180+
However, the directive name is often a poor choice for the the name of a property within the directive class.
1181+
The directive name rarely describes what the property does.
1182+
The `myClick` directive name is not a good name for a property that emits click messages.
11801183

11811184
Fortunately, we can have a public name for the property that meets conventional expectations
1182-
and use a different name internally
1183-
by providing a public alias for the internal property name in the decorator like this:
1185+
while using a different name internally.
1186+
In the example immediately above, we are actually binding *through the* `myClick` *alias* to
1187+
the directive's own `clicks` property.
1188+
1189+
We can specify the alias for the property name by passing it into the input/output decorator like this:
1190+
11841191
+makeExample('template-syntax/ts/app/my-click.directive.ts', 'my-click-output-1')(format=".")
11851192
:marked
1186-
Now the directive name, `myClick`, is the public facing property name to which we can bind
1187-
+makeExample('template-syntax/ts/app/app.component.html', 'my-click')(format=".")
1188-
:marked
1189-
while inside the directive, the property is known as `clicks`.
1193+
11901194

1191-
With aliasing we please both the directive consumer and the directive author.
11921195
.l-sub-section
11931196
:marked
1194-
We can alias property names in the `inputs` and `outputs` arrays as well.
1195-
We write a colon-delimited string with
1196-
the internal property name on the left and the public name on the right:
1197+
We can also alias property names in the `inputs` and `outputs` arrays.
1198+
We write a *colon-delimited* (:) string with
1199+
the directive property name on the *left* and the public alias on the *right*:
11971200
+makeExample('template-syntax/ts/app/my-click.directive.ts', 'my-click-output-2')(format=".")
11981201

11991202
<a id="expression-operators"></a>

0 commit comments

Comments
 (0)