Krishna
Krishna

Reputation: 1209

How to call ngOnInit for the second time?

In my header.component.ts I'm storing the value and navigating to profile page

this.global.profilevalue = value;
this.router.navigate(["/profile"]);

And in profile.component.ts in ngOnInit, I'm comparing that value and calling the corresponding function

if(this.global.profilevalue == "abc")
{
    this.abc();
}
else {
    this.efg();
}

The above part is working only for the first time, When I'm in /profile and clicking on some profile value it is not taking to ngOnInit in profile.

How can I access ngOnInit even the route is /profile

Upvotes: 0

Views: 2761

Answers (1)

Pardeep Jain
Pardeep Jain

Reputation: 86730

You can But It is not good practice to call ngOnInit many times , instead you can use some function on component load so that you can call that conditionally too

like this

ngOnInit() {
  this.initLoad();
}

initLoad() {
  if(this.global.profilevalue == "abc") {
      this.abc();
  } else {
      this.efg();
  }
}

Upvotes: 2

Related Questions