Angular 4 - The Basics 笔记(3):Databinding
-
String Interpolation
out put variables in the template file
- add variables in name.component.ts
export class HeaderComponent{ //property appTitle: string = 'Angular'; version: number = 4; //method getVersion(){ return this.version; } }
- edit name.component.html template file to use the data
<h3>{{ appTitle }}_{{ getVersion() }}</h3>
-
Property Binding
-
edit name.component.ts
status = false;
- edit name.component.html
<button [disabled]="status">Button</button> <!-- or do this--> <img [src]="bannerImg">
-
Event Binding
- edit name.component.ts
changeMessage(){ this.message = 'new content has been posted!'; } updateContent(event: any){ this.content = event.target.value; }
- edit name.component.html
<p>message: <i> {{message}}</i></p> <p>preview: <i>{{content}}</i></p> <input type="text" (input)="updateContent($event)"> <button (click)="changeMessage()">post</button>
-
Two-Way-Binding
- edit name.component.html
<input type="text" class="form-control" [(ngModel)]="content"> <p>{{content}}</p>
- edit name.component.ts
content: string = 'empty';