Here is my Angular 2 app:
//root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<div class="{{item.itemclass}}" id="lists" *ngFor="let item of items">
<div class="item" (click)="selectItem(item)">{{item}}</div>
<div>{{item.itemclass}}</div>
</div>
</div>
`,
})
export class App {
name:string;
items: any[];
itemclass : string;
constructor() {
this.name = 'Angular2';
this.items = [
'lorem',
'ipsum',
'sit',
'trump :D'
];
this.itemclass = '';
}
selectItem(item){
console.log(item);
item.itemclass = "active";
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
When the click event selectItem is triggered, it is supposed to change the itemclass. However, nothing happens.
Find my app at Plunker.
All I want to achieve is: when a user clicks on a item it would have an active css selector, and when another item is clicked, it would simply toggle and all the siblings will not have the active css class any more.
But it isn't working properly. How to do it in Angular 2?