0
我試圖在按鈕按下時按下數組中的數據,並在按鈕再次按下一次(並更改按鈕類)時將其從數組中刪除。如何切換按鈕?
<button
*ngFor="#color of colors"
[class.selected]="color === selectedColors"
(click)="onPress(color)">
{{color.name}}
</button>
我把我的類中的兩種方法 - onPress & hasColor
onPress(color: Color) {
this.selectedColors = color;
// ckecking color in array
if (this.hasColor(this.colorsArray,this.selectedColors.id)) {
//need to splice a color from array and change background color to default (to blue color)
} else {
// if not in colorsArray then push and change class to .selected
this.colorsArray.push(this.selectedColors.id);
}
}
hasColor(arrayC,id) {
arrayC.forEach(function(item) {
if (item === id) {
return true;
}
return false;
});
}
我該怎麼辦呢? hasColor方法返回true或false,但從來沒有在onPress方法返回
非常感謝。我需要學習Javascript(((( – Slip