I have an application that I am developing in Angular 2 (RC1). The menu must be created from the database. Data is delivered via Web Api in JSON form. I would like to create a menu from the data recursively to be sure that the depth of the menu is not a problem.
The problem is that I want to add the class to a specific line of the ngFor loop, and the class is added to all the lines, not just the one I want.
The code looks something like this:
sidenav.component.ts
import { Component, Input } from '@angular/core'; import { IMenu } from '../../../shared/models/menu.interface'; import { MenuComponent } from './menu.component'; @Component({ moduleId: module.id, selector: 'sidenav', templateUrl: 'sidenav.component.html', directives: [MenuComponent] }) export class SidenavComponent { @Input() menu: IMeni[] }
sidenav.component.html
... <menu-view [menu]="menu"></menu-view> ...
menu.component.ts
import { Component, Input } from '@angular/core'; import { IMenu } from '../../../shared/models/menu.interface'; @Component({ moduleId: module.id, selector: 'menu-view', templateUrl: 'menu.component.html', directives: [MenuComponent] }) export class MenuComponent { isSelected: boolean = false; @Input() meni: IMeni[]; onSelect(): void { this.isSelected = !this.isSelected; } }
menu.component.html
<ul> <li *ngFor="let item of menu; let frst=first" class="menu-list" [ngClass]="{'active': 'isSelected', 'active': 'frst'}"> <a [routerLink]="[item.uri]" (click)="onSelect()" > {{item.name}}</a> <meni-view [menu]="item.children"></meni-view> </li> </ul>
So, when I click on the parent, all the parents become active, and not just what will be, which will be a satisfactory behavior. What am I doing wrong?
angular
Igor Ilić
source share