我有一堆列表項目,並希望突出每個單擊它。對於我來說,在jQuery甚至JavaScript中做這件事很容易,但是當談到Angular2時,我迷失了方向。Angular2 - 添加課程到點擊項目
<ul>
<li [attr.data-selected]="false" (click)="highlightItem($event)" [class.highlight]="isHighlighted($event)" *ngFor="#item of items"> {{item}} </li>
</ul>
我的組件
export class HelloWorld {
items = ["pineapples", "apples", "tomatoes", "bread"];
highlightItem(event) {
event.target.setAttribute("data-selected", "true");
}
isHighlighted(event) {
return event.target.getAttribute("data-selected") == "true";
}
}
不知道在哪裏我的錯誤是,或者如果我使用了錯誤的方法
這確實對我來說不太工作,但我相信你是正確的。可能與蹲跳者和我的版本角度 – user5680735
抱歉,忘記提及。你的代碼看起來像Angular 2 beta('#item of items','#'在測試版中使用)。 Mine - ** Angular 2 RC1 ** –
無用的評論反對stackoverflow規則:超! – Luther