如果您需要通过改变ngstyle选择各个按钮的,这是我做的。
btn.component.html
<div *ngIf="socketData && socketData.status === 'ok'">
<div *ngFor="let button of socketData.message; let i = index"
[ngStyle]="hovered === i ? pressedStyle(button) : buttonStyle(button)"
(mouseover)="hovered = i"
(mouseout)="hovered = -1"
(click)="reqTicket(button.id)">
{{button.someImportantData}} - {{button.yetMoreImportantData}}
</div>
</div>
btn.component.ts
export class ButtonComponent implements OnInit {
style;
hovered;
...
private buttonStyle(button) {
let btnType = this.setBtnType(button);
this.style = {
'color': '#' + button.textColor,
'font-size': button.textSize + 'px',
'background-color': btnType.background
};
return this.style;
}
private pressedStyle(button) {
let pressed = this.setBtnType(button, this.hovered);
this.style['background-color'] = pressed.background;
return this.style;
}
private setBtnType(button, hovered?) {
let type = 'normal';
let btn = {
normal: {
background: '#' + button.backColor,
},
pressed: {
background: '#' + button.backColor,
}
}
if(hovered > -1) type = 'pressed';
return {
border: btn[type].width + ' ' + btn[type].color + ' ' + 'solid',
background: btn[type].background
};
}
}
希望这可以帮助别人:)
什么是'悬停'这里'[ngStyle] =“悬停{' ? –
目前尚不清楚你想要什么。如果你想切换悬停样式,请将以下内容添加到按钮'
我试图复制CSS .. .. .logout-container button:hover { } 希望为悬停状态设置按钮的颜色。我可以使用 .logout-container button:hover {颜色:#FFFFFF!important; background-color:rgba(0,0,0,0.00)!重要; border-color:#FFFFFF!important; } – user2182570