我正在使用Angular-cli点击角度2捕获按钮属性
我有一个简单的数组,我循环并输出为按钮。
我加入循环索引作为一个属性上的按钮
如何获取按钮属性的点击时。
// component
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
blocks: string[] = ['Red','Green','Blue','Yellow','White'];
constructor(){}
clickEvent($event){
alert($event.target.attr.data-index);
}
}
// component.html
<div class="blocks">
<div *ngFor="let block of blocks; let i=index">
<button (click)="clickEvent($event)" [attr.data-index]="i">{{block}}</button>
</div>
</div>