在Angular中使用动态CSS的解决方法有很多种,下面是其中一种示例:
This is a dynamically styled element
export class MyComponent {
bgColor: string = 'red';
textColor: string = 'white';
changeStyles() {
this.bgColor = 'blue';
this.textColor = 'yellow';
}
}
div {
background-color: red;
color: white;
}
这样,当组件加载时,元素的背景色将为红色,文本颜色为白色。当调用changeStyles()
方法时,元素的背景色将变为蓝色,文本颜色变为黄色。