在Angular中,你可以使用类方法来改变类变量/属性。下面是一个使用函数来改变类变量的示例:
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
template: `
{{ myVariable }}
`
})
export class ExampleComponent {
myVariable: string = 'Initial Value';
changeVariable() {
this.myVariable = 'New Value';
}
}
在上面的示例中,我们定义了一个名为myVariable
的类变量,并将其初始值设置为'Initial Value'
。然后,我们在模板中创建了一个按钮,并使用(click)
事件绑定将changeVariable()
方法绑定到按钮上。当按钮被点击时,changeVariable()
方法会被调用,将myVariable
的值改为'New Value'
。最后,我们将myVariable
的值显示在标签中。
这样,当用户点击按钮时,myVariable
的值将被改变,并在模板中进行更新。