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

Commit 64a8754

Browse files
chalinwardbell
authored andcommitted
example(template-syntax): follow style-guide and other updates (#2750)
1 parent 7619cdf commit 64a8754

File tree

7 files changed

+86
-57
lines changed

7 files changed

+86
-57
lines changed

public/docs/_examples/template-syntax/e2e-spec.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ describe('Template Syntax', function () {
3131
expect(specialButtonEle.getAttribute('style')).toMatch('color: red');
3232
});
3333

34-
it('should two-way bind to sizer', function () {
35-
let buttons = element.all(by.css('div#two-way-1 my-sizer button'));
36-
let input = element(by.css('input#fontsize'));
37-
38-
input.getAttribute('value').then(size => {
39-
buttons.get(1).click();
40-
browser.waitForAngular();
41-
expect(input.getAttribute('value')).toEqual((+size + 1).toString());
42-
});
34+
it('should two-way bind to sizer', async () => {
35+
let div = element(by.css('div#two-way-1'));
36+
let incButton = div.element(by.buttonText('+'));
37+
let input = div.element(by.css('input'));
38+
let initSize = await input.getAttribute('value');
39+
incButton.click();
40+
expect(input.getAttribute('value')).toEqual((+initSize + 1).toString());
4341
});
4442
});
43+

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

+20-7
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ <h3>
305305

306306
<div>
307307
<!-- #docregion event-binding-3 -->
308-
<!-- `myClick` is an event on the custom `MyClickDirective` -->
309-
<!-- #docregion my-click -->
308+
<!-- `myClick` is an event on the custom `ClickDirective` -->
309+
<!-- #docregion myClick -->
310310
<div (myClick)="clickMessage=$event">click with myClick</div>
311-
<!-- #enddocregion my-click -->
311+
<!-- #enddocregion myClick -->
312312
<!-- #enddocregion event-binding-3 -->
313313
{{clickMessage}}
314314
</div>
@@ -351,21 +351,22 @@ <h3>
351351
<hr><h2 id="two-way">Two-way Binding</h2>
352352
<div id="two-way-1">
353353
<!-- #docregion two-way-1 -->
354-
<my-sizer [(size)]="fontSize"></my-sizer>
355-
<div [style.font-size.px]="fontSize">Resizable Text</div>
354+
<my-sizer [(size)]="fontSizePx"></my-sizer>
355+
<div [style.font-size.px]="fontSizePx">Resizable Text</div>
356356
<!-- #enddocregion two-way-1 -->
357-
<label>FontSize: <input id="fontsize" [(ngModel)]="fontSize"></label>
357+
<label>FontSize (px): <input [(ngModel)]="fontSizePx"></label>
358358
</div>
359359
<br>
360360
<div id="two-way-2">
361361
<h3>De-sugared two-way binding</h3>
362362
<!-- #docregion two-way-2 -->
363-
<my-sizer [size]="fontSize" (sizeChange)="fontSize=$event"></my-sizer>
363+
<my-sizer [size]="fontSizePx" (sizeChange)="fontSizePx=$event"></my-sizer>
364364
<!-- #enddocregion two-way-2 -->
365365
</div>
366366
<br><br>
367367

368368
<a class="to-toc" href="#toc">top</a>
369+
369370
<!-- Two way data binding unwound;
370371
passing the changed display value to the event handler via `$event` -->
371372
<hr><h2 id="ngModel">NgModel (two-way) Binding</h2>
@@ -428,6 +429,18 @@ <h3>Result: {{currentHero.firstName}}</h3>
428429
<!-- NgStyle binding -->
429430
<hr><h2 id="ngStyle">NgStyle Binding</h2>
430431

432+
<!-- #docregion NgStyle -->
433+
<div>
434+
<p [ngStyle]="setStyle()" #styleP>Change style of this text!</p>
435+
436+
<label>Italic: <input type="checkbox" [(ngModel)]="isItalic"></label> |
437+
<label>Bold: <input type="checkbox" [(ngModel)]="isBold"></label> |
438+
<label>Size: <input type="text" [(ngModel)]="fontSize"></label>
439+
440+
<p>Style set to: <code>'{{styleP.style.cssText}}'</code></p>
441+
</div>
442+
<!-- #enddocregion NgStyle -->
443+
431444
<!-- #docregion NgStyle-1 -->
432445
<div [style.font-size]="isSpecial ? 'x-large' : 'smaller'" >
433446
This div is x-large.

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ export class AppComponent implements AfterViewInit, OnInit {
5050
this.alert('Deleted hero: ' + (hero && hero.firstName));
5151
}
5252

53-
fontSize = 10;
54-
5553
// #docregion evil-title
5654
evilTitle = 'Template <script>alert("evil never sleeps")</script>Syntax';
5755
// #enddocregion evil-title
@@ -180,6 +178,21 @@ export class AppComponent implements AfterViewInit, OnInit {
180178
}
181179
// #enddocregion setStyles
182180

181+
// #docregion NgStyle
182+
isItalic = false;
183+
isBold = false;
184+
fontSize: string = 'large';
185+
fontSizePx: number | string = 14;
186+
187+
setStyle() {
188+
return {
189+
'font-style': this.isItalic ? 'italic' : 'normal',
190+
'font-weight': this.isBold ? 'bold' : 'normal',
191+
'font-size': this.fontSize
192+
};
193+
}
194+
// #enddocregion NgStyle
195+
183196
toeChoice = '';
184197
toeChooser(picker: HTMLFieldSetElement) {
185198
let choices = picker.children;

public/docs/_examples/template-syntax/ts/app/app.module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FormsModule } from '@angular/forms';
44

55
import { AppComponent } from './app.component';
66
import { BigHeroDetailComponent, HeroDetailComponent } from './hero-detail.component';
7-
import { MyClickDirective, MyClickDirective2 } from './my-click.directive';
7+
import { ClickDirective, ClickDirective2 } from './click.directive';
88
import { SizerComponent } from './sizer.component';
99

1010
@NgModule({
@@ -16,8 +16,8 @@ import { SizerComponent } from './sizer.component';
1616
AppComponent,
1717
BigHeroDetailComponent,
1818
HeroDetailComponent,
19-
MyClickDirective,
20-
MyClickDirective2,
19+
ClickDirective,
20+
ClickDirective2,
2121
SizerComponent
2222
],
2323
bootstrap: [ AppComponent ]

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import { Directive, ElementRef, EventEmitter, Output } from '@angular/core';
44

55
@Directive({selector: '[myClick]'})
6-
export class MyClickDirective {
7-
// #docregion my-click-output-1
6+
export class ClickDirective {
7+
// #docregion output-myClick
88
@Output('myClick') clicks = new EventEmitter<string>(); // @Output(alias) propertyName = ...
9-
// #enddocregion my-click-output-1
9+
// #enddocregion output-myClick
1010

1111
toggle = false;
1212

@@ -19,15 +19,15 @@ export class MyClickDirective {
1919
}
2020
}
2121

22-
// #docregion my-click-output-2
22+
// #docregion output-myClick2
2323
@Directive({
24-
// #enddocregion my-click-output-2
24+
// #enddocregion output-myClick2
2525
selector: '[myClick2]',
26-
// #docregion my-click-output-2
26+
// #docregion output-myClick2
2727
outputs: ['clicks:myClick'] // propertyName:alias
2828
})
29-
// #enddocregion my-click-output-2
30-
export class MyClickDirective2 {
29+
// #enddocregion output-myClick2
30+
export class ClickDirective2 {
3131
clicks = new EventEmitter<string>();
3232
toggle = false;
3333

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
1111
</div>`
1212
})
1313
export class SizerComponent {
14-
@Input() size: number;
14+
@Input() size: number | string;
1515
@Output() sizeChange = new EventEmitter<number>();
1616

1717
dec() { this.resize(-1); }
1818
inc() { this.resize(+1); }
1919

2020
resize(delta: number) {
21-
const size = +this.size + delta;
22-
this.size = Math.min(40, Math.max(8, size));
21+
this.size = Math.min(40, Math.max(8, +this.size + delta));
2322
this.sizeChange.emit(this.size);
2423
}
2524
}

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

+30-25
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ table
490490
If we must read a target element property or call one of its methods,
491491
we'll need a different technique.
492492
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).
495495

496496
:marked
497497
### Binding target
@@ -581,7 +581,7 @@ a(id="one-time-initialization")
581581

582582

583583
:marked
584-
#### Content Security
584+
#### Content security
585585
Imagine the following *malicious content*.
586586
+makeExample('template-syntax/ts/app/app.component.ts', 'evil-title')(format=".")
587587
:marked
@@ -599,10 +599,10 @@ figure.image-display
599599
.l-main-section
600600
:marked
601601
<a id="other-bindings"></a>
602-
## Attribute, Class, and Style Bindings
602+
## Attribute, class, and style bindings
603603
The template syntax provides specialized one-way bindings for scenarios less well suited to property binding.
604604

605-
### Attribute Binding
605+
### Attribute binding
606606
We can set the value of an attribute directly with an **attribute binding**.
607607
.l-sub-section
608608
:marked
@@ -652,7 +652,7 @@ code-example(format="nocode").
652652
is to set ARIA attributes, as in this example:
653653
+makeExample('template-syntax/ts/app/app.component.html', 'attrib-binding-aria')(format=".")
654654
:marked
655-
### Class Binding
655+
### Class binding
656656

657657
We can add and remove CSS class names from an element’s `class` attribute with
658658
a **class binding**.
@@ -668,9 +668,6 @@ code-example(format="nocode").
668668
We can replace that with a binding to a string of the desired class names; this is an all-or-nothing, replacement binding.
669669
+makeExample('template-syntax/ts/app/app.component.html', 'class-binding-2')(format=".")
670670

671-
block dart-class-binding-bug
672-
//- N/A
673-
674671
:marked
675672
Finally, we can bind to a specific class name.
676673
Angular adds the class when the template expression evaluates to #{_truthy}.
@@ -683,7 +680,7 @@ block dart-class-binding-bug
683680
we generally prefer the [NgClass directive](#ngClass) for managing multiple class names at the same time.
684681

685682
:marked
686-
### Style Binding
683+
### Style binding
687684

688685
We can set inline styles with a **style binding**.
689686

@@ -747,7 +744,7 @@ block style-property-name-dart-diff
747744
on [aliasing input/output properties](#aliasing-io).
748745

749746
: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,
751748
Angular reports an “unknown directive” error.
752749

753750
### *$event* and event handling statements
@@ -778,7 +775,7 @@ block style-property-name-dart-diff
778775

779776
<a id="eventemitter"></a>
780777
<a id="custom-event"></a>
781-
### Custom Events with EventEmitter
778+
### Custom events with *EventEmitter*
782779

783780
Directives typically raise custom events with an Angular [EventEmitter](../api/core/index/EventEmitter-class.html).
784781
The directive creates an `EventEmitter` and exposes it as a property.
@@ -853,36 +850,44 @@ block style-property-name-dart-diff
853850

854851
Angular offers a special _two-way data binding_ syntax for this purpose, **`[(x)]`**.
855852
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+
857855
.callout.is-important
858856
header [( )] = banana in a box
859857
:marked
860858
Visualize a *banana in a box* to remember that the parentheses go _inside_ the brackets.
859+
861860
:marked
862861
The `[(x)]` syntax is easy to demonstrate when the element has a settable property called `x`
863862
and a corresponding event named `xChange`.
864863
Here's a `SizerComponent` that fits the pattern.
865864
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+
867868
:marked
868869
The initial `size` is an input value from a property binding.
869870
Clicking the buttons increases or decreases the `size`, within min/max values constraints,
870871
and then raises (_emits_) the `sizeChange` event with the adjusted size.
871872

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+
874877
: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>.
879882

880883
The two-way binding syntax is really just syntactic sugar for a _property_ binding and an _event_ binding.
881884
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+
883888
:marked
884889
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.
886891

887892
Clearly the two-way binding syntax is a great convenience compared to separate property and event bindings.
888893

@@ -1418,7 +1423,7 @@ h3#aliasing-io Aliasing input/output properties
14181423
Directive consumers expect to bind to the name of the directive.
14191424
For example, when we apply a directive with a `myClick` selector to a `<div>` tag,
14201425
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=".")
14221427
:marked
14231428
However, the directive name is often a poor choice for the name of a property within the directive class.
14241429
The directive name rarely describes what the property does.
@@ -1431,14 +1436,14 @@ h3#aliasing-io Aliasing input/output properties
14311436

14321437
We can specify the alias for the property name by passing it into the input/output decorator like this:
14331438

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=".")
14351440

14361441
.l-sub-section
14371442
:marked
14381443
We can also alias property names in the `inputs` and `outputs` #{_array}s.
14391444
We write a colon-delimited (`:`) string with
14401445
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=".")
14421447

14431448
<a id="expression-operators"></a>
14441449
.l-main-section

0 commit comments

Comments
 (0)