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

Commit 5af06f9

Browse files
committed
docs(template-syntax): clarify [()] de-sugaring
also changed anchors to use id instead of name
1 parent a2cb803 commit 5af06f9

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ table
542542
// #docregion property-binding-7
543543
:marked
544544
### Binding target
545-
A name between enclosing square brackets identifies the target property. The target property in the following code is the image element’s `src` property.
545+
a id between enclosing square brackets identifies the target property. The target property in the following code is the image element’s `src` property.
546546

547547
// #enddocregion property-binding-7
548548
+makeExample('template-syntax/ts/app/app.component.html', 'property-binding-1')(format=".")
@@ -901,7 +901,7 @@ code-example(format="", language="html").
901901
// #docregion ngModel-1
902902
.l-main-section
903903
:marked
904-
<a name="ngModel"></a>
904+
<a id="ngModel"></a>
905905
## Two-way binding with NgModel
906906
When developing data entry forms, we often want to both display a data property and update that property when the user makes changes.
907907

@@ -929,15 +929,22 @@ code-example(format="", language="html").
929929
+makeExample('template-syntax/ts/app/app.component.html', 'without-NgModel')(format=".")
930930
// #docregion ngModel-4
931931
:marked
932-
That’s cumbersome. Who can remember what element property to set and what event reports user changes?
932+
That’s cumbersome. Who can remember which element property to set and what event reports user changes?
933933
How do we extract the currently displayed text from the input box so we can update the data property?
934934
Who wants to look that up each time?
935935

936-
That `ngModel` directive hides these onerous details. It wraps the element’s `value` property, listens to the `input` event,
937-
and raises its own event with the changed text ready for us to consume.
936+
That `ngModel` directive hides these onerous details behind its own `ngModel` input and `ngModelChange` output properties.
938937
// #enddocregion ngModel-4
939938
+makeExample('template-syntax/ts/app/app.component.html', 'NgModel-3')(format=".")
940939
// #docregion ngModel-5
940+
.l-sub-section
941+
:marked
942+
The `ngModel` input property sets the element's value property and the `ngModelChange` output property
943+
listens for changes to the element's value.
944+
The details are specific to each kind of element and therefore the `NgModel` directive only works for elements
945+
that implement one of a few patterns such as the input box's `value` property and `change` event.
946+
We can't simply apply `[(ngModel)]` to our custom components unless we write them to conform.
947+
941948
:marked
942949
That’s an improvement, but it could be better.
943950

@@ -949,16 +956,18 @@ code-example(format="", language="html").
949956
// #docregion ngModel-6
950957
.l-sub-section
951958
:marked
952-
Internally, Angular maps the term `ngModel` to an `ngModel` input property and an
953-
`ngModelChange` output property.
954-
That’s a specific example of a more general pattern in which Angular matches `[(x)]` to an `x` input property
955-
for property binding and an `xChange` output property for event binding.
959+
`[(ngModel)]` is a specific example of a more general pattern in which Angular "de-sugars" the `[(x)]` syntax
960+
into an `x` input property for property binding and an `xChange` output property for event binding.
961+
Angular constructs the event property binding's template statement by appending `=$event`
962+
to the literal string of the template expression.
963+
code-example(format="." ).
964+
[(x)]="hero.name" &lt;==> [x]="hero.name" (xChange)="hero.name=$event"
965+
956966
:marked
957967
Is `[(ngModel)]` all we need? Is there ever a reason to fall back to its expanded form?
958968

959-
Well, `NgModel` can only set the target property.
960-
What if we need to do something more or something different when the user changes the value?
961-
Then we need to use the expanded form.
969+
The `[( )]` syntax can only set the data-bound property when the value changes.
970+
If we need to do something more or something different, we need to write the expanded form ourselves.
962971

963972
Let's try something silly like forcing the input value to uppercase:
964973
// #enddocregion ngModel-6
@@ -973,7 +982,7 @@ figure.image-display
973982
// #docregion directives-1
974983
.l-main-section
975984
:marked
976-
<a name="directives"></a>
985+
<a id="directives"></a>
977986
## Built-in directives
978987

979988
Earlier versions of Angular included over seventy built-in directives.
@@ -1249,8 +1258,8 @@ figure.image-display
12491258
// #enddocregion directives-ngForTrackBy-3
12501259
12511260
// #docregion star-template
1252-
<a name="star-template"></a>
1253-
<a name="structural-directive"></a>
1261+
<a id="star-template"></a>
1262+
<a id="structural-directive"></a>
12541263
.l-main-section
12551264
:marked
12561265
## * and &lt;template&gt;

0 commit comments

Comments
 (0)