@@ -542,7 +542,7 @@ table
542
542
// #docregion property-binding-7
543
543
:marked
544
544
### 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.
546
546
547
547
// #enddocregion property-binding-7
548
548
+ makeExample('template-syntax/ts/app/app.component.html' , 'property-binding-1' )( format ="." )
@@ -901,7 +901,7 @@ code-example(format="", language="html").
901
901
// #docregion ngModel-1
902
902
.l-main-section
903
903
:marked
904
- <a name ="ngModel"></a>
904
+ <a id ="ngModel"></a>
905
905
## Two-way binding with NgModel
906
906
When developing data entry forms, we often want to both display a data property and update that property when the user makes changes.
907
907
@@ -929,15 +929,22 @@ code-example(format="", language="html").
929
929
+ makeExample('template-syntax/ts/app/app.component.html' , 'without-NgModel' )( format ="." )
930
930
// #docregion ngModel-4
931
931
: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?
933
933
How do we extract the currently displayed text from the input box so we can update the data property?
934
934
Who wants to look that up each time?
935
935
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.
938
937
// #enddocregion ngModel-4
939
938
+ makeExample('template-syntax/ts/app/app.component.html' , 'NgModel-3' )( format ="." )
940
939
// #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
+
941
948
:marked
942
949
That’s an improvement, but it could be better.
943
950
@@ -949,16 +956,18 @@ code-example(format="", language="html").
949
956
// #docregion ngModel-6
950
957
.l-sub-section
951
958
: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" < ==> [x]="hero.name" (xChange)="hero.name=$event"
965
+
956
966
:marked
957
967
Is `[(ngModel)]` all we need? Is there ever a reason to fall back to its expanded form?
958
968
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.
962
971
963
972
Let's try something silly like forcing the input value to uppercase:
964
973
// #enddocregion ngModel-6
@@ -973,7 +982,7 @@ figure.image-display
973
982
// #docregion directives-1
974
983
.l-main-section
975
984
:marked
976
- <a name ="directives"></a>
985
+ <a id ="directives"></a>
977
986
## Built-in directives
978
987
979
988
Earlier versions of Angular included over seventy built-in directives.
@@ -1249,8 +1258,8 @@ figure.image-display
1249
1258
// #enddocregion directives-ngForTrackBy-3
1250
1259
1251
1260
// #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 >
1254
1263
.l-main-section
1255
1264
:marked
1256
1265
## * and <template>
0 commit comments