- Interpolation
- Property Binding
- Event Binding
- Two-way Binding
- Class Binding
- Style Binding
- Event Binding
import FormsModule
Interpolation { { } }
-
app.component.ts
1
2
3export class AppComponent {
pageTitle = 'helloworld';
} -
app.component.html
1
<h1>{{ pageTitle }}</h1>
Property Binding []
Property Binding 1
-
app.component.ts
1
2
3export class AppComponent {
imgUrl = 'https://picsum.photos/200';
} -
app.component.html
1
2
3<div>
<img [src]="imgUrl" alt="Image" />
</div>
Property Binding 2
-
test.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34@Component({
selector: 'app-test',
template: `
<h2>
Welcome {{ name }}
</h2>
<input [id]="myId" type="text" value="NickName" />
<input id="{{myId}}" type="text" value="NickName" />
<input disabled="false" type="text" value="NickName" />
<input disabled="{{false}}" type="text" value="NickName" />
<input [disabled]="isDisabled" type="text" value="NickName" />
<input bind-disabled="isDisabled" type="text" value="NickName" />
<button (click)="onChangeDisabled()">Click</button>
`,
styleUrls: []
})
export class TestComponent implements OnInit {
public name = "Hello World";
public myId = "testId";
public isDisabled = true;
() { }
ngOnInit() {
}
onChangeDisabled() {
this.isDisabled = !this.isDisabled;
}
} -
app.component.html
1
<app-test></app-test>
Event Binding ()
-
app.component.ts
1
2
3
4
5
6
7export class AppComponent {
count = 0;
incrementCount() {
this.count += 1;
}
} -
app.component.html
1
2
3
4<div>
<button (click)="incrementCount()">Click</button>
<p>Clicked {{ count }} times</p>
</div>
Two-way Binding [()]
Two-way Binding 1
-
app.component.ts
1
2
3export class AppComponent {
userName: string;
} -
app.component.html
1
2
3
4<div>
<input type="text" [ngModel]="userName" (ngModelChange)="userName=$event">
<p>Welcome {{ userName }}</p>
</div>
Two-way Binding 2
-
app.component.ts
1
2
3
4
5
6
7
8
9
10
11export class AppComponent {
nickName: string;
greetNickName(updatedValue) {
this.nickName = updatedValue;
if(updatedValue == 'Nickname'){
alert('Welcome back Nickname');
}
}
} -
app.component.html
1
2
3
4<div>
<input type="text" [ngModel]="nickName" (ngModelChange)="greetNickName($event)">
<p>Welcome {{ nickName }}</p>
</div>
Two-way Binding 3
-
app.component.ts
1
2
3export class AppComponent {
name: string;
} -
app.component.html
1
2
3
4<div>
<input type="text" [(ngModel)]="name">
<p>Welcome {{ name }}</p>
</div>
Class Binding
-
test.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51@Component({
selector: 'app-test',
template: `
<h2>
Welcome {{ name }}
</h2>
<h2 class="text-success">Hello</h2>
<h2 [class]="successClass">Hello</h2>
<h2 class="text-special" [class]="successClass">Hello</h2> <!--text-special not work-->
<h2 [class.text-danger]="hasError">Hello</h2> <!--single class-->
<h2 [ngClass]="messageClasses">Hello</h2> <!--multiple classes-->
<button (click)="onChangeClass()">Click</button>
`,
styles: [`
.text-success {
color: green;
}
.text-danger {
color: red;
}
.text-special {
font-style: italic;
}
`]
})
export class TestComponent implements OnInit {
public name = "Hello World";
public successClass = "text-success"
public hasError = false;
public isSpecial = true;
public messageClasses = {
"text-success": !this.hasError,
"text-danger": this.hasError,
"text-special": this.isSpecial
}
() { }
ngOnInit() {
}
onChangeClass() {
this.hasError = !this.hasError;
this.messageClasses["text-danger"] = this.hasError;
}
} -
app.component.html
1
<app-test></app-test>
Style Binding
-
test.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36@Component({
selector: 'app-test',
template: `
<h2>
Welcome {{ name }}
</h2>
<h2 [style.color]="'orange'">Style Binding</h2>
<h2 [style.color]="hasError ? 'red' : 'green'">Style Binding</h2>
<h2 [style.color]="highlightColor">Style Binding 2</h2>
<h2 [ngStyle]="titleStyles">Style Binding 3</h2>
<button (click)="onChangeClass()">Click</button>
`,
styles: []
})
export class TestComponent implements OnInit {
public name = "Hello World";
public hasError = false;
public highlightColor = "orange";
public titleStyles = {
color: "blue",
fontStyle: "italic"
}
() { }
ngOnInit() {
}
onChangeClass() {
this.hasError = !this.hasError;
}
} -
app.component.html
1
<app-test></app-test>
Event Binding
-
test.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38@Component({
selector: 'app-test',
template: `
<h2>
Welcome {{ name }}
</h2>
<button (click)="onClick()">Click Function</button>
{{ greeting }}
<button (click)="onClickEvent($event)">Click Event</button>
{{ greetingEvent }}
<button (click)="greeting = 'Hello'">Click</button>
`,
styles: []
})
export class TestComponent implements OnInit {
public name = "Hello World";
public greeting = "";
public greetingEvent = "";
() { }
ngOnInit() {
}
onClick() {
console.log(this.name);
this.greeting = this.name;
}
onClickEvent(event) {
console.log(event);
this.greetingEvent = event.type;
}
} -
app.component.html
1
<app-test></app-test>
Reference
Angular Component Interaction - 3 - Binding
Angular 8 Tutorial - 6 - Property Binding
Angular 8 Tutorial - 7 - Class Binding
Angular 8 Tutorial - 8 - Style Binding
Angular 8 Tutorial - 9 - Event Binding
近期评论