I am currently working with an angular2 component that generates a list of chapters
using an *ngFor=
tag. However, I am facing an issue where I am unable to individually target these chapters in my ng2 component to highlight the selected chapter. I expected the code snippet below to produce something like this:
<p class="chapter 1" #1>1. My First Chapter</p>
But unfortunately, the #1 is not being generated as expected, resulting in my selector not functioning properly and preventing me from setting the first chapter in the list as selected by default.
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'tutorial',
template: `
<div class="content row">
<div class="chapters col s3">
<h3>Chapters:</h3>
<p *ngFor="let chapter of chapters" class="chapter" #{{chapter.number}}>{{chapter.number}}. {{chapter.title}}</p>
</div>
</div>
`
})
export class bookComponent implements AfterViewInit {
public chapters = _chapters;
@ViewChild('2') el:ElementRef;
ngAfterViewInit() {
this.el.nativeElement.className += " clicked";
}
}
What steps should I take to be able to individually select the dynamically generated <p>
tags?