14
14
15
15
:marked
16
16
The Angular application manages what the user sees and can do, achieving this through the interaction of a
17
- Component class instance (the *component*) and its user-facing template.
17
+ component class instance (the *component*) and its user-facing template.
18
18
19
19
You may be familiar with the component/template duality from your experience with model-view-controller (MVC) or model-view-viewmodel (MVVM).
20
20
In Angular, the component plays the part of the controller/viewmodel, and the template represents the view.
@@ -76,7 +76,7 @@ code-example(language="html" escape="html").
76
76
You can extend the HTML vocabulary of your templates with components and directives that appear as new elements and attributes.
77
77
In the following sections, you'll learn how to get and set DOM (Document Object Model) values dynamically through data binding.
78
78
79
- Begin with the first form of data binding — interpolation — to see how much richer template HTML can be.
79
+ Begin with the first form of data binding—interpolation—to see how much richer template HTML can be.
80
80
81
81
a( href ="#toc" ) back to top
82
82
@@ -147,6 +147,12 @@ block notable-differences
147
147
148
148
h3#expression-context Expression context
149
149
150
+ .alert.is-critical
151
+ :marked
152
+ Ward: you wanted to have a note here per our slack conversation where you said,
153
+ "The context for terms in an expression is a blend of the data-bound component
154
+ and the directive's context (if it has one). The latter wins when the term is a property of both."
155
+
150
156
:marked
151
157
The *expression context* is typically the component instance.
152
158
In the following examples, the *title* within double-curly braces, `{{title}}` is a property of the data-bound component.
@@ -381,7 +387,7 @@ table(width="100%")
381
387
+ makeExample('template-syntax/ts/app/app.component.html' , 'disabled-button-1' )( format ="." )
382
388
:marked
383
389
You'll get to that peculiar bracket notation in a moment. Looking beyond it,
384
- your intuition suggest that you're binding to the button's `disabled` attribute and setting
390
+ your intuition suggests that you're binding to the button's `disabled` attribute and setting
385
391
it to the current value of the component's `isUnchanged` property.
386
392
387
393
Your intuition is incorrect! Your everyday HTML mental model is misleading.
@@ -442,7 +448,7 @@ table(width="100%")
442
448
When you write a data binding, you're dealing exclusively with properties and events of the target object.
443
449
HTML attributes effectively disappear.
444
450
:marked
445
- With this model firmly in mind, let's learn about binding targets.
451
+ With this model firmly in mind, read on to learn about binding targets.
446
452
447
453
### Binding targets
448
454
The **target of a data binding** is something in the DOM.
@@ -601,7 +607,7 @@ block dart-type-exceptions
601
607
:marked
602
608
### Remember the brackets
603
609
The brackets tell Angular to evaluate the template expression.
604
- If directive forget the brackets, Angular treats the string as a constant and *initializes the target property* with that string.
610
+ If you omit the brackets, Angular treats the string as a constant and *initializes the target property* with that string.
605
611
It does *not* evaluate the string!
606
612
607
613
Don't make the following mistake:
@@ -615,7 +621,7 @@ a(id="one-time-initialization")
615
621
### One-time string initialization
616
622
You *should* omit the brackets when all of the following are true:
617
623
* The target property accepts a string value.
618
- * The string is a fixed value that directive can bake into the template.
624
+ * The string is a fixed value that you can bake into the template.
619
625
* This initial value never changes.
620
626
621
627
You routinely initialize attributes this way in standard HTML, and it works
@@ -785,7 +791,7 @@ a(href="#toc") back to top
785
791
a#event-binding
786
792
:marked
787
793
## Event binding ( <span class="syntax">(event)</span> )
788
- The bindings directive 've met so far flow data in one direction: **from a component to an element**.
794
+ The bindings directives you 've met so far flow data in one direction: **from a component to an element**.
789
795
790
796
Users don't just stare at the screen. They enter text into input boxes. They pick items from lists.
791
797
They click buttons. Such user actions may result in a flow of data in the opposite direction:
@@ -840,6 +846,12 @@ a#event-binding
840
846
841
847
Consider this example:
842
848
+ makeExample('template-syntax/ts/app/app.component.html' , 'without-NgModel' )( format ="." )
849
+
850
+ .alert.is-critical
851
+ :marked
852
+ Ward: `firstName` appears here (below) for the first time without any mention it was coming and isn't in the
853
+ code sample. Do we mean `name` instead?
854
+
843
855
:marked
844
856
This code sets the input box `value` property by binding to the `firstName` property. To listen for changes to the value, the code binds to the input box's `input` event.
845
857
When the user makes changes, the `input` event is raised, and the binding executes the statement within a context that includes the DOM event object, `$event`.
@@ -967,7 +979,7 @@ a#two-way
967
979
968
980
Clearly the two-way binding syntax is a great convenience compared to separate property and event bindings.
969
981
970
- You'd like to use two-way binding with HTML form elements like `<input>` and `<select>`.
982
+ It would be convenient to use two-way binding with HTML form elements like `<input>` and `<select>`.
971
983
However, no native HTML element follows the `x` value and `xChange` event pattern.
972
984
973
985
Fortunately, the Angular [_NgModel_](#ngModel) directive is a bridge that enables two-way binding to form elements.
@@ -985,7 +997,7 @@ a#directives
985
997
986
998
You don't need many of those directives in Angular.
987
999
You can often achieve the same results with the more capable and expressive Angular binding system.
988
- Why create a directive to handle a click when directive can write a simple binding such as this?
1000
+ Why create a directive to handle a click when you can write a simple binding such as this?
989
1001
+ makeExample('template-syntax/ts/app/app.component.html' , 'event-binding-1' )( format ="." )
990
1002
:marked
991
1003
You still benefit from directives that simplify complex tasks.
@@ -1036,7 +1048,7 @@ a#ngClass
1036
1048
:marked
1037
1049
Consider a `setCurrentClasses` component method that sets a component property,
1038
1050
`currentClasses` with an object that adds or removes three classes based on the
1039
- `true`/`false` state of three other component propertes :
1051
+ `true`/`false` state of three other component properties :
1040
1052
+ makeExample('template-syntax/ts/app/app.component.ts' , 'setClasses' )( format ="." )
1041
1053
:marked
1042
1054
Adding an `ngClass` property binding to `currentClasses` sets the element's classes accordingly:
@@ -1078,7 +1090,8 @@ a(href="#toc") back to top
1078
1090
a#ngModel
1079
1091
:marked
1080
1092
### NgModel - Two-way binding to form elements with <span class="syntax">[(ngModel)]</span>
1081
- When developing data entry forms, directive often both display a data property and update that property when the user makes changes.
1093
+ When developing data entry forms, you often both display a data property and
1094
+ update that property when the user makes changes.
1082
1095
1083
1096
Two-way data binding with the `NgModel` directive makes that easy. Here's an example:
1084
1097
+ makeExcerpt('app/app.component.html' , 'NgModel-1' , '' )
@@ -1088,7 +1101,7 @@ a#ngModel
1088
1101
#### _FormsModule_ is required to use _ngModel_
1089
1102
1090
1103
Before using the `ngModel` directive in a two-way data binding,
1091
- directive must import the `FormsModule` and add it to the Angular module's `imports` list.
1104
+ you must import the `FormsModule` and add it to the Angular module's `imports` list.
1092
1105
Learn more about the `FormsModule` and `ngModel` in the
1093
1106
[Forms](../guide/forms.html#ngModel) guide.
1094
1107
@@ -1123,15 +1136,15 @@ a#ngModel
1123
1136
You can't apply `[(ngModel)]` to a non-form native element or a third-party custom component until you write a suitable *value accessor*,
1124
1137
a technique that is beyond the scope of this guide.
1125
1138
1126
- You don't need a _value accessor_ for an Angular component that you write ... because you can name the value and event properties
1139
+ You don't need a _value accessor_ for an Angular component that you write because you can name the value and event properties
1127
1140
to suit Angular's basic [two-way binding syntax](#two-way) and skip `NgModel` altogether.
1128
1141
The [`sizer` shown above](#two-way) is an example of this technique.
1129
1142
1130
1143
:marked
1131
1144
Separate `ngModel` bindings is an improvement over binding to the element's native properties. You can do better.
1132
1145
1133
1146
You shouldn't have to mention the data property twice. Angular should be able to capture the component's data property and set it
1134
- with a single declaration — which it can with the `[(ngModel)]` syntax:
1147
+ with a single declaration, which it can with the `[(ngModel)]` syntax:
1135
1148
+ makeExample('template-syntax/ts/app/app.component.html' , 'NgModel-1' )( format ="." )
1136
1149
:marked
1137
1150
Is `[(ngModel)]` all you need? Is there ever a reason to fall back to its expanded form?
@@ -1370,7 +1383,7 @@ figure.image-display
1370
1383
Each component has a `hero` [input property](#inputs-outputs "Input property")
1371
1384
which is bound to the `currentHero` of the parent component.
1372
1385
1373
- Switch directives work as well with native elements and Web Components too.
1386
+ Switch directives work as well with native elements and web components too.
1374
1387
For example, you could replace the `<confused-hero>` switch case with the following.
1375
1388
+ makeExample('template-syntax/ts/app/app.component.html' , 'NgSwitch-div' )( format ="." )
1376
1389
@@ -1386,7 +1399,7 @@ a#ref-var
1386
1399
:marked
1387
1400
A **template reference variable** is often a reference to a DOM element within a template.
1388
1401
It can also be a reference to an Angular component or directive or a
1389
- <a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components" target="_blank" title="MDN: Web Components">Web Component </a>.
1402
+ <a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components" target="_blank" title="MDN: Web Components">web component </a>.
1390
1403
1391
1404
Use the hash symbol (#) to declare a reference variable.
1392
1405
The `#phone` declares a `phone` variable on an `<input>` element.
@@ -1418,7 +1431,7 @@ a#ref-var
1418
1431
The `heroForm` is actually a reference to an Angular [NgForm](../api/forms/index/NgForm-directive.html "API: NgForm")
1419
1432
directive with the ability to track the value and validity of every control in the form.
1420
1433
1421
- The native `<form>` element doesn't have a `form` propery .
1434
+ The native `<form>` element doesn't have a `form` property .
1422
1435
But the `NgForm` directive does, which explains how you can disable the submit button
1423
1436
if the `heroForm.form.valid` is invalid and pass the entire form control tree
1424
1437
to the parent component's `onSubmit` method.
@@ -1477,6 +1490,15 @@ a#inputs-outputs
1477
1490
:marked
1478
1491
They are *neither inputs nor outputs* of the component. They are data sources for their bindings.
1479
1492
1493
+ .alert.is-critical
1494
+ :marked
1495
+ Ward: I was momentarily uncertain below because the instructions say to look
1496
+ at `HeroDetailComponent` but it doesn't occur explicitly in the code sample.
1497
+ In the following paragraph I then see `HeroDetailComponent.hero` and think,
1498
+ "hm, ok, I guess that means we're inside `HeroDetailComponent`", but my
1499
+ assumption feels wobbly.
1500
+
1501
+ :marked
1480
1502
Now look at `HeroDetailComponent` when it is the **target of a binding**.
1481
1503
+ makeExample('template-syntax/ts/app/app.component.html' , 'io-2' )( format ="." )
1482
1504
:marked
0 commit comments