Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if router-outlet is in use

All,

I would like to use *ngIf to remove a section of a page if a specific router-outlet has a component.

I've tried searching through ActivatedRoute and Router, but I can't seem to figure it out. How do you check if a specific router-outlet is in use?

like image 373
Dan Grahn Avatar asked Dec 08 '17 14:12

Dan Grahn


2 Answers

Can be solved like this:

<div [hidden]="!myOutlet.isActivated">
  <router-outlet name="my-outlet" #myOutlet="outlet"></router-outlet>
</div>

No changes in component class needed.

like image 166
martsraits Avatar answered Oct 18 '22 02:10

martsraits


I figured this out. You need to use the events for router outlets. However, you can't use ngIf because events won't be fired unless the outlet is in the dom.

ts

showOutlet: boolean;

onActivate(event : any) {
  this.showOutlet = true;
}

onDeactivate(event : any) {
  this.showOutlet = false;
}

html

<div [hidden]="!showOutlet">
  <router-outlet name="my-outlet"
      (activate)="onActivate($event)"
      (deactivate)="onDeactivate($event)"></router-outlet>
</div>
like image 4
Dan Grahn Avatar answered Oct 18 '22 00:10

Dan Grahn