Ali Hasani
Ali Hasani

Reputation: 165

How to detect a new angular component is loaded in layout component?

We have an angular project that uses "router-outlet" to render components into layout (component) .. As you know, the layout's ngOnInit runs out only for the first time of load and when a new component loads into the layout, layout's ngOnInit doesn't run again .. I'm looking for to know how to detect a new component is loaded in the layout .. Is there a way to know ?!

Upvotes: 1

Views: 3651

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 19622

Make use of activate event for the same . Whenever we load a component in router outlet a activate event is emitted .

Layout Component

  <div class="container">
    <router-outlet (activate)="onActivate($event)"></router-outlet>
  </div>

Template

  onActivate(componentRef){
    // fires every time a new component is loaded
  }

Upvotes: 3

Related Questions