Router
поможет вам достичь ожидаемого поведения.
<ul>
<li [routerLink]="routes.home"
[ngClass]="{ 'activated': currentTab === routes.home}">
Home
</li>
<li [routerLink]="routes.placeholder"
[ngClass]="{ 'activated': currentTab === routes.placeholder}">
PlaceHolder
</li>
</ul>
<router-outlet></router-outlet>
import { Component } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { filter, map } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
currentTab = '';
readonly routes = {
home: '/home',
placeholder: '/placeholder'
};
constructor(private router: Router) {
this.router.events
.pipe(
filter(e => e instanceof NavigationEnd),
map((e: NavigationEnd) => e.url)
)
.subscribe(newUrl => {
this.currentTab = newUrl;
});
}
}
Вы можете проверить, как это работает здесь