I have the following code in an angular2 template. It generates a list of chapters and sets the clicked property on the one that is the current chapter.
<div class="chapters col s3">
<a *ngFor="let chapter of chapters; let i=index" (click)="clickedItem = i" [class.clicked]="i == clickedItem" class="chapter"
[routerLink]="['/tutorial/chapter', chapter.number]">{{chapter.number}}. {{chapter.title}} </a>
</div>
The issue is that this is only populated when the page is initially loaded, if the chapter changes, the router doesn't refresh this component, so the "clicked" chapter doesn't change.
I thought maybe I could use *ngIf
and bind it to the chapter
property of my component, which is subscribed to the route params, hence this updates itself. Then, select either a version with the clicked class or not. However, this implementation throws the error:
<div class="chapters col s3">
<a *ngFor="let chapter of chapters; let i=index" *ngIf="i==chapter" (click)="clickedItem = i" class="chapter clicked" [routerLink]="['/tutorial/chapter', chapter.number]">{{chapter.number}}. {{chapter.title}} </a>
<a *ngFor="let chapter of chapters; let i=index" *ngIf="i==chapter" (click)="clickedItem != i" class="chapter" [routerLink]="['/tutorial/chapter', chapter.number]">{{chapter.number}}. {{chapter.title}} </a>
</div>
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with *
I can't see how wrapping this in a div with the ngIf property would work, as I need the i
from the ngFor
?
Any ideas how I could make this work?