可以使用Angular内置的双向数据绑定实现文本的动态改变。在HTML模板中,使用双括号'{{}}”来绑定变量和表达式,然后在控制器中更新变量的值即可。
示例代码:
HTML模板:
{{text}}
控制器:
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my.component.html'
})
export class MyComponent {
text: string = 'Hello, world!';
changeText() {
this.text = 'Welcome to Angular!';
}
}
当点击'Change Text”按钮时,控制器中的changeText方法会被调用,更新text变量的值,而HTML模板中绑定的文本内容也会相应地改变。
上一篇:Angular改变输入值并显示
下一篇:Angular改变显示的值