Angular 2 ngForループインデックスに基づいてクラス名を追加する際に問題が発生しています。 ngを使用して複数のボタンを作成する場合、ボタンをクリックすると、特定のボタンのクラスが追加されます。 Linkループ内のクラス名のクリック項目を追加する* ngFor Angular 2
<button color="light" *ngFor="let category of categories" [class.tagactive]='stateOfButton' (click)="changeState(i)" let i = index;>{{category}} <span (click)="delete(i)">X</span></button>
stateOfButton: boolean = false;
categories = ['Windstorm', 'Bombasto', 'Magneta', 'Tornado', 'mango'];
changeState(index) {
this.stateOfButton = !this.stateOfButton;
}
delete(index) {
this.categories.splice(index, 1);
}
.tagactive { background: #cc3333; color:#fff;}
だから何?私は働くか、それとも問題ないのですか? Btw、 'let i = index;'は '* ngFor'の中になければなりません。 – martin
ボタンをクリックすると全てのボタンが追加されます。http://www.jqueryscript.net/demo/Simple-Mobile-Friendly-jQuery-Tags-Input-Plugin-Taxonomy/のような色を変更するには特定のボタンが必要です –