@@ -490,8 +490,8 @@ table
490
490
If we must read a target element property or call one of its methods,
491
491
we'll need a different technique.
492
492
See the API reference for
493
- [viewChild ](../api/core/index/ViewChild-decorator.html) and
494
- [contentChild ](../api/core/index/ContentChild-decorator.html).
493
+ [ViewChild ](../api/core/index/ViewChild-decorator.html) and
494
+ [ContentChild ](../api/core/index/ContentChild-decorator.html).
495
495
496
496
:marked
497
497
### Binding target
@@ -581,7 +581,7 @@ a(id="one-time-initialization")
581
581
582
582
583
583
:marked
584
- #### Content Security
584
+ #### Content security
585
585
Imagine the following *malicious content*.
586
586
+ makeExample('template-syntax/ts/app/app.component.ts' , 'evil-title' )( format ="." )
587
587
:marked
@@ -599,10 +599,10 @@ figure.image-display
599
599
.l-main-section
600
600
:marked
601
601
<a id="other-bindings"></a>
602
- ## Attribute, Class , and Style Bindings
602
+ ## Attribute, class , and style bindings
603
603
The template syntax provides specialized one-way bindings for scenarios less well suited to property binding.
604
604
605
- ### Attribute Binding
605
+ ### Attribute binding
606
606
We can set the value of an attribute directly with an **attribute binding**.
607
607
.l-sub-section
608
608
:marked
@@ -652,7 +652,7 @@ code-example(format="nocode").
652
652
is to set ARIA attributes, as in this example:
653
653
+ makeExample('template-syntax/ts/app/app.component.html' , 'attrib-binding-aria' )( format ="." )
654
654
:marked
655
- ### Class Binding
655
+ ### Class binding
656
656
657
657
We can add and remove CSS class names from an element’s `class` attribute with
658
658
a **class binding**.
@@ -668,9 +668,6 @@ code-example(format="nocode").
668
668
We can replace that with a binding to a string of the desired class names; this is an all-or-nothing, replacement binding.
669
669
+ makeExample('template-syntax/ts/app/app.component.html' , 'class-binding-2' )( format ="." )
670
670
671
- block dart-class-binding-bug
672
- //- N/A
673
-
674
671
:marked
675
672
Finally, we can bind to a specific class name.
676
673
Angular adds the class when the template expression evaluates to #{_truthy}.
@@ -683,7 +680,7 @@ block dart-class-binding-bug
683
680
we generally prefer the [NgClass directive](#ngClass) for managing multiple class names at the same time.
684
681
685
682
:marked
686
- ### Style Binding
683
+ ### Style binding
687
684
688
685
We can set inline styles with a **style binding**.
689
686
@@ -747,7 +744,7 @@ block style-property-name-dart-diff
747
744
on [aliasing input/output properties](#aliasing-io).
748
745
749
746
:marked
750
- If the name fails to match element event or output property of a known directive,
747
+ If the name fails to match an element event or an output property of a known directive,
751
748
Angular reports an “unknown directive” error.
752
749
753
750
### *$event* and event handling statements
@@ -778,7 +775,7 @@ block style-property-name-dart-diff
778
775
779
776
<a id="eventemitter"></a>
780
777
<a id="custom-event"></a>
781
- ### Custom Events with EventEmitter
778
+ ### Custom events with * EventEmitter*
782
779
783
780
Directives typically raise custom events with an Angular [EventEmitter](../api/core/index/EventEmitter-class.html).
784
781
The directive creates an `EventEmitter` and exposes it as a property.
@@ -853,36 +850,44 @@ block style-property-name-dart-diff
853
850
854
851
Angular offers a special _two-way data binding_ syntax for this purpose, **`[(x)]`**.
855
852
The `[(x)]` syntax combines the brackets
856
- of _Property Binding_, `[x]`, with the parentheses of _Event Binding_, `(x)`.
853
+ of _property binding_, `[x]`, with the parentheses of _event binding_, `(x)`.
854
+
857
855
.callout.is-important
858
856
header [( )] = banana in a box
859
857
:marked
860
858
Visualize a *banana in a box* to remember that the parentheses go _inside_ the brackets.
859
+
861
860
:marked
862
861
The `[(x)]` syntax is easy to demonstrate when the element has a settable property called `x`
863
862
and a corresponding event named `xChange`.
864
863
Here's a `SizerComponent` that fits the pattern.
865
864
It has a `size` value property and a companion `sizeChange` event:
866
- + makeExample('template-syntax/ts/app/sizer.component.ts' , null , 'app/sizer.component.ts' )
865
+
866
+ + makeExample('app/sizer.component.ts' )
867
+
867
868
:marked
868
869
The initial `size` is an input value from a property binding.
869
870
Clicking the buttons increases or decreases the `size`, within min/max values constraints,
870
871
and then raises (_emits_) the `sizeChange` event with the adjusted size.
871
872
872
- Here's an example in which the `AppComponent.fontSize` is two-way bound to the `SizerComponent`:
873
- + makeExample('template-syntax/ts/app/app.component.html' , 'two-way-1' )( format ="." )
873
+ Here's an example in which the `AppComponent.fontSizePx` is two-way bound to the `SizerComponent`:
874
+
875
+ + makeExcerpt('app/app.component.html' , 'two-way-1' , '' )
876
+
874
877
:marked
875
- The `AppComponent.fontSize ` establishes the initial `SizerComponent.size` value.
876
- Clicking the buttons updates the `AppComponent.fontSize ` via the two-way binding.
877
- The revised `AppComponent.fontSize ` value flows through to the _style_ binding, making the displayed text bigger or smaller.
878
- Try it in the <live-example>live example </live-example>.
878
+ The `AppComponent.fontSizePx ` establishes the initial `SizerComponent.size` value.
879
+ Clicking the buttons updates the `AppComponent.fontSizePx ` via the two-way binding.
880
+ The revised `AppComponent.fontSizePx ` value flows through to the _style_ binding, making the displayed text bigger or smaller.
881
+ Try it in the <live-example></live-example>.
879
882
880
883
The two-way binding syntax is really just syntactic sugar for a _property_ binding and an _event_ binding.
881
884
Angular _desugars_ the `SizerComponent` binding into this:
882
- + makeExample('template-syntax/ts/app/app.component.html' , 'two-way-2' )( format ="." )
885
+
886
+ + makeExcerpt('app/app.component.html' , 'two-way-2' , '' )
887
+
883
888
:marked
884
889
The `$event` variable contains the payload of the `SizerComponent.sizeChange` event.
885
- Angular assigns the `$event` value to the `AppComponent.fontSize ` when the user clicks the buttons.
890
+ Angular assigns the `$event` value to the `AppComponent.fontSizePx ` when the user clicks the buttons.
886
891
887
892
Clearly the two-way binding syntax is a great convenience compared to separate property and event bindings.
888
893
@@ -1418,7 +1423,7 @@ h3#aliasing-io Aliasing input/output properties
1418
1423
Directive consumers expect to bind to the name of the directive.
1419
1424
For example, when we apply a directive with a `myClick` selector to a `<div>` tag,
1420
1425
we expect to bind to an event property that is also called `myClick`.
1421
- + makeExample('template-syntax/ts/app/app.component.html' , 'my-click ' )( format ="." )
1426
+ + makeExample('template-syntax/ts/app/app.component.html' , 'myClick ' )( format ="." )
1422
1427
:marked
1423
1428
However, the directive name is often a poor choice for the name of a property within the directive class.
1424
1429
The directive name rarely describes what the property does.
@@ -1431,14 +1436,14 @@ h3#aliasing-io Aliasing input/output properties
1431
1436
1432
1437
We can specify the alias for the property name by passing it into the input/output decorator like this:
1433
1438
1434
- + makeExample('template-syntax/ts/app/my- click.directive.ts' , 'my-click- output-1 ' )( format ="." )
1439
+ + makeExample('template-syntax/ts/app/click.directive.ts' , 'output-myClick ' )( format ="." )
1435
1440
1436
1441
.l-sub-section
1437
1442
:marked
1438
1443
We can also alias property names in the `inputs` and `outputs` #{_array}s.
1439
1444
We write a colon-delimited (`:`) string with
1440
1445
the directive property name on the *left* and the public alias on the *right*:
1441
- + makeExample('template-syntax/ts/app/my- click.directive.ts' , 'my-click- output-2 ' )( format ="." )
1446
+ + makeExample('template-syntax/ts/app/click.directive.ts' , 'output-myClick2 ' )( format ="." )
1442
1447
1443
1448
<a id =" expression-operators" ></a >
1444
1449
.l-main-section
0 commit comments