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

Commit 795a690

Browse files
committed
example(template-syntax): follow style-guide and other updates
- Follow style guide: rename `sizer.component.ts` to `my-sizer.component.ts` (given the selector `my-sizer`); also appropriately rename class. - Rename `AppComponent.fontSize` to `AppComponent.fontSizePx` because it is assigned to `[style.font-size.px]` and so represents a size in px, not a general font-size value (which could be, e.g., “x-large”). - Added small interactive example of `NgStyle`.
1 parent e813fb4 commit 795a690

File tree

6 files changed

+65
-36
lines changed

6 files changed

+65
-36
lines changed

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

Lines changed: 8 additions & 9 deletions
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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 15 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FormsModule } from '@angular/forms';
55
import { AppComponent } from './app.component';
66
import { BigHeroDetailComponent, HeroDetailComponent } from './hero-detail.component';
77
import { MyClickDirective, MyClickDirective2 } from './my-click.directive';
8-
import { SizerComponent } from './sizer.component';
8+
import { MySizerComponent } from './my-sizer.component';
99

1010
@NgModule({
1111
imports: [
@@ -18,7 +18,7 @@ import { SizerComponent } from './sizer.component';
1818
HeroDetailComponent,
1919
MyClickDirective,
2020
MyClickDirective2,
21-
SizerComponent
21+
MySizerComponent
2222
],
2323
bootstrap: [ AppComponent ]
2424
})

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
1010
<label [style.font-size.px]="size">FontSize: {{size}}px</label>
1111
</div>`
1212
})
13-
export class SizerComponent {
14-
@Input() size: number;
13+
export class MySizerComponent {
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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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}.
@@ -854,35 +851,43 @@ block style-property-name-dart-diff
854851
Angular offers a special _two-way data binding_ syntax for this purpose, **`[(x)]`**.
855852
The `[(x)]` syntax combines the brackets
856853
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`.
864-
Here's a `SizerComponent` that fits the pattern.
863+
Here's a `MySizerComponent` 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/my-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 `MySizerComponent`:
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 `MySizerComponent.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.
881-
Angular _desugars_ the `SizerComponent` binding into this:
882-
+makeExample('template-syntax/ts/app/app.component.html', 'two-way-2')(format=".")
884+
Angular _desugars_ the `MySizerComponent` binding into this:
885+
886+
+makeExcerpt('app/app.component.html', 'two-way-2', '')
887+
883888
:marked
884-
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.
889+
The `$event` variable contains the payload of the `MySizerComponent.sizeChange` event.
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

0 commit comments

Comments
 (0)