First, convert the fragment observable from ActivatedRoute to a signal, using toSignal:
export class AppComponent {
route = inject(ActivatedRoute);
fragment = toSignal(this.route.fragment);
name = 'Angular';
ids: Array<String> = ['one', 'two', 'three', 'four'];
}
Then, we simply, use the string method .includes(...) to check if the ID matches the current fragment.
<section>
<div
class="section"
*ngFor="let link of ids"
[attr.id]="link"
[ngClass]="{ 'is-active-section': fragment() === link }"
>
<h1>{{ link }}</h1>
<div class="content">
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Incidunt saepe
exercitationem praesentium modi repudiandae dicta consectetur ab veritatis
quaerat quibusdam quod, dolorem fugit temporibus voluptas magni ad odio,
adipisci doloremque!
</div>
</div>
</section>
Full code:
HTML:
<hello name="{{ name }}"></hello>
<div class="links">
<ul>
<li *ngFor="let link of ids">
<a [routerLink]="'.'" [fragment]="link">{{ link }}</a>
</li>
</ul>
</div>
{{ fragment() }}
<section>
<div
class="section"
*ngFor="let link of ids"
[attr.id]="link"
[ngClass]="{ 'is-active-section': fragment() === link }"
>
<h1>{{ link }}</h1>
<div class="content">
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Incidunt saepe
exercitationem praesentium modi repudiandae dicta consectetur ab veritatis
quaerat quibusdam quod, dolorem fugit temporibus voluptas magni ad odio,
adipisci doloremque!
</div>
</div>
</section>
TS:
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute } from '@angular/router';
import { delay } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: false,
})
export class AppComponent {
route = inject(ActivatedRoute);
fragment = toSignal(this.route.fragment);
name = 'Angular';
ids: Array<String> = ['one', 'two', 'three', 'four'];
}
Introduce delay to end the styling:
Same as the above code, here we use linkedSignal, which is computed from a source signal, but we can change the value.
export class AppComponent {
route = inject(ActivatedRoute);
fragment = toSignal(this.route.fragment);
timedFragment = linkedSignal(() => this.fragment());
Then using effect we trigger a setTimeout to fire after 2000s then reset the linkedSignal, until a new fragment arrives and the process is triggered again.
constructor() {
effect(() => {
if (this.fragment()) {
setTimeout(() => {
this.timedFragment.set('');
}, 2000);
}
});
}
ActivatedRoute'sfragmentobservable and setting class may work for you. In same place you can set timeout to remove class. I guess that's workaround.<p …>; same as I would style with:target. But if I style something else, that is an OK work-around.