0

I'm trying to redirect from one page to another using routerLink, but the redirected link doesn't trigger the ngOnInit() method as well as the ActivatedRoute subscription. The redirected page then freezes but there is no error on the console.

So this is my HTML

<a [routerLink]="route(value.id)">link</i></a>

Then this is my component.ts, which returns the url

route = (id: number) => this.routeService.view(id);

This is the redirected component edit.ts

ngOnInit() {
  this.route.paramMap.pipe(
    takeUntil(this.onDestroy$),
    map(params => console.log(params))
).subscribe(x => 'do something')

}

Jin
  • 162
  • 2
  • 14

1 Answers1

1

I did a little hack, I used NgZone. routerLink directive seems to run outside Angular zone. So instead of using [routerLink]="route(value.id)" I used (click)="route(value.id)" . So this is how it looks like now

HTML:

<a (click)="route(value.id)">link</i></a>

Component:

constructor(private ngZone: NgZone){}

route = (id: number) { 
    const link = this.routeService.view(id);
    this.ngZone.run(() => this.router.navigate(link));
}

Thanks for replying..

Jin
  • 162
  • 2
  • 14