docs: add example and edit two-way-binding section of Template Syntax (#26278)
PR Close #26278
This commit is contained in:

committed by
Andrew Kushnir

parent
85d38ae564
commit
7e3a60ad31
@ -0,0 +1,17 @@
|
||||
<h1 id="two-way">Two-way Binding</h1>
|
||||
<div id="two-way-1">
|
||||
<!-- #docregion two-way-1 -->
|
||||
<app-sizer [(size)]="fontSizePx"></app-sizer>
|
||||
<div [style.font-size.px]="fontSizePx">Resizable Text</div>
|
||||
<!-- #enddocregion two-way-1 -->
|
||||
<label>FontSize (px): <input [(ngModel)]="fontSizePx"></label>
|
||||
</div>
|
||||
<br>
|
||||
<div id="two-way-2">
|
||||
<h2>De-sugared two-way binding</h2>
|
||||
<!-- #docregion two-way-2 -->
|
||||
<app-sizer [size]="fontSizePx" (sizeChange)="fontSizePx=$event"></app-sizer>
|
||||
<!-- #enddocregion two-way-2 -->
|
||||
</div>
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
import { TestBed, async } from '@angular/core/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
describe('AppComponent', () => {
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [
|
||||
AppComponent
|
||||
],
|
||||
}).compileComponents();
|
||||
}));
|
||||
it('should create the app', async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.debugElement.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
}));
|
||||
it(`should have as title 'app'`, async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.debugElement.componentInstance;
|
||||
expect(app.title).toEqual('app');
|
||||
}));
|
||||
it('should render title in a h1 tag', async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
const compiled = fixture.debugElement.nativeElement;
|
||||
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
|
||||
}));
|
||||
});
|
@ -0,0 +1,13 @@
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.css']
|
||||
})
|
||||
export class AppComponent {
|
||||
constructor() { }
|
||||
// #docregion font-size
|
||||
fontSizePx = 16;
|
||||
// #enddocregion font-size
|
||||
}
|
22
aio/content/examples/two-way-binding/src/app/app.module.ts
Normal file
22
aio/content/examples/two-way-binding/src/app/app.module.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { NgModule } from '@angular/core';
|
||||
|
||||
|
||||
import { AppComponent } from './app.component';
|
||||
import { SizerComponent } from './sizer/sizer.component';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent,
|
||||
SizerComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
FormsModule
|
||||
],
|
||||
providers: [],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
export class AppModule { }
|
@ -0,0 +1,5 @@
|
||||
<div>
|
||||
<button (click)="dec()" title="smaller">-</button>
|
||||
<button (click)="inc()" title="bigger">+</button>
|
||||
<label [style.font-size.px]="size">FontSize: {{size}}px</label>
|
||||
</div>
|
@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { SizerComponent } from './sizer.component';
|
||||
|
||||
describe('SizerComponent', () => {
|
||||
let component: SizerComponent;
|
||||
let fixture: ComponentFixture<SizerComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ SizerComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(SizerComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
@ -0,0 +1,22 @@
|
||||
import { Component, Input, Output, EventEmitter } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-sizer',
|
||||
templateUrl: './sizer.component.html',
|
||||
styleUrls: ['./sizer.component.css']
|
||||
})
|
||||
export class SizerComponent {
|
||||
|
||||
|
||||
@Input() size: number | string;
|
||||
@Output() sizeChange = new EventEmitter<number>();
|
||||
|
||||
dec() { this.resize(-1); }
|
||||
inc() { this.resize(+1); }
|
||||
|
||||
resize(delta: number) {
|
||||
this.size = Math.min(40, Math.max(8, +this.size + delta));
|
||||
this.sizeChange.emit(this.size);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user