fix(router): RouterLinkActive should update its state right after checking the children (#19449)

Closes #18983

PR Close #19449
This commit is contained in:
vsavkin
2017-09-27 16:44:17 -04:00
committed by Tobias Bosch
parent 01e4aa5427
commit c569b75249
3 changed files with 68 additions and 16 deletions

View File

@ -118,21 +118,19 @@ export class RouterLinkActive implements OnChanges,
private update(): void {
if (!this.links || !this.linksWithHrefs || !this.router.navigated) return;
const hasActiveLinks = this.hasActiveLinks();
// react only when status has changed to prevent unnecessary dom updates
if (this.isActive !== hasActiveLinks) {
this.classes.forEach((c) => {
if (hasActiveLinks) {
this.renderer.addClass(this.element.nativeElement, c);
} else {
this.renderer.removeClass(this.element.nativeElement, c);
}
});
Promise.resolve(hasActiveLinks).then(active => (this as{
isActive: boolean
}).isActive = active);
}
Promise.resolve().then(() => {
const hasActiveLinks = this.hasActiveLinks();
if (this.isActive !== hasActiveLinks) {
(this as any).isActive = hasActiveLinks;
this.classes.forEach((c) => {
if (hasActiveLinks) {
this.renderer.addClass(this.element.nativeElement, c);
} else {
this.renderer.removeClass(this.element.nativeElement, c);
}
});
}
});
}
private isLinkActive(router: Router): (link: (RouterLink|RouterLinkWithHref)) => boolean {