Angular 使用角度选择多个按钮 ` {{value}}`
单击后,我需要更改所选按钮的背景色。Angular 使用角度选择多个按钮 ` {{value}}`,angular,button,colors,background,angular9,Angular,Button,Colors,Background,Angular9,单击后,我需要更改所选按钮的背景色。 为click事件创建带有HostListener的指令 注入元素引用和渲染器2 在主机侦听器方法中,调用renderr2.addClass(this.elemenentRef,'class with color for button') 为click事件创建带有HostListener的指令 注入元素引用和渲染器2 在主机侦听器方法中,调用renderr2.addClass(this.elemenentRef,'class with color for b
- 为click事件创建带有HostListener的指令
- 注入元素引用和渲染器2
- 在主机侦听器方法中,调用renderr2.addClass(this.elemenentRef,'class with color for button')
- 为click事件创建带有HostListener的指令
- 注入元素引用和渲染器2
- 在主机侦听器方法中,调用renderr2.addClass(this.elemenentRef,'class with color for button')
{{value}}
如果要切换类,请使用toggle
而不是add
{{value}
演示这只是另一种方式,但创建指令是适当的方式,如Alexander答案
{{value}}
如果要切换类,请使用toggle
而不是add
{{value}
演示
对于多选选项
演示URL:
对于多选选项
演示URL:例如,如果您想打印日期并添加切换功能,您的html应该是
<div *ngFor="let day of weekDays; let i = index" style="display: inline-block;">
<button mat-mini-fab [color]="day.status ? 'primary' : ''"
(click)="ChangeWeekDayStatus(i)">
{{day.text}}</button> </div>
</div>
和一个处理切换的函数
ChangeWeekDayStatus(index:number){
this.weekDays[index].status=!this.weekDays[index].status;
}
例如,如果您想打印日期并添加切换功能,您的html应该是
<div *ngFor="let day of weekDays; let i = index" style="display: inline-block;">
<button mat-mini-fab [color]="day.status ? 'primary' : ''"
(click)="ChangeWeekDayStatus(i)">
{{day.text}}</button> </div>
</div>
和一个处理切换的函数
ChangeWeekDayStatus(index:number){
this.weekDays[index].status=!this.weekDays[index].status;
}
到目前为止你尝试了什么?到目前为止你尝试了什么?我们可以双击切换回正常吗?是的,这是可能的,但在这种情况下,最好按照Alexander的答案创建方向检查我的更新答案,你可以使用切换而不是添加,这将切换类我们可以双击切换回正常吗?是的,这是可能的,但在这种情况下case最好通过创建directivecheck my updated answer来遵循Alexander的答案,您可以使用toggle而不是add。这将切换类
ChangeWeekDayStatus(index:number){
this.weekDays[index].status=!this.weekDays[index].status;
}