How to call ngOnInit() again in Angular 2?
Asked Answered
I

5

24

Please explain in this code, how to call ngOnInit() again when I call another method?

ngOnInit(): void {
  this.route.params.subscribe((params: Params) => {
    this.model = this.userData;
  });
}

update() {
  this.loading = true;
  this.userService.update(this.model).subscribe(
    (data) => {
      alert('Update successful');
    },
    (error) => {
      alert('Not updated');
      this.loading = false;
    },
  );
  this.user_data();
}
Implantation answered 6/7, 2017 at 7:41 Comment(2)
What's the purpose? Just move the code to another method and call that method from ngOnInit(). ngOnInit() is called once for every method. There is no way to make it being called multiple times.Blida
please explain me for updated codeImplantation
C
41

There are two options from my point of view:

Calling ngOnInit() from another function scope. But I would suggest to do not adopt this approach given ngOnInitis an angular core method that belongs to OnInit Interface.

    public ngOnInit() {
          this.route.params.subscribe((params: Params) => {
          this.model=this.userData;
      });      
    }
    
    update() {
           this.ngOnInit();
    }  

Break your functionality into another function, use ngOnInitto call it and, afterwards, any request can be made from anywhere by calling the function in the following manner: this.<MethodName>();.

    public ngOnInit() {
          this.getRouteData();
    }
    
    update() {
           this.getRouteData(); 
    }

    getRouteData() {
      this.route.params.subscribe((params: Params) => {
          this.model=this.userData;
      }); 
    }    
Cloninger answered 6/7, 2017 at 8:4 Comment(0)
M
18

If the purpose is to trigger ngOnInit() when query param is updated, then do following:

import { Router } from '@angular/router';
constructor(private router: Router) {
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
}
Mehalick answered 19/3, 2019 at 17:18 Comment(2)
This is exactly the solution I was looking for. Big thank you HariRoumell
But this will affect if you have routerLink to go in other routes it will not work. Tested by myselfJoyance
G
4

ngOnInit called once the component is created. so you can create a function and call the function again. Here is the sample code.

ngOnInit(): void {
    this.callFun();
}    

update() {
    this.callFun();
    // do code
}

private callFun(){
   // do code
}
Gorgon answered 6/7, 2017 at 8:0 Comment(0)
S
4

You should not need to call ngOnInit again. So the question remains, what are you really trying to accomplish? Are you having issues retrieving the routing parameter?

 ngOnInit(): void {
      this.route.params.subscribe((params: Params) => {
        this.model=this.userData;
  }); 

Your code within the subscribe above will AUTOMATICALLY be re-executed every time the route parameters change. So there is no need to manually call ngOnInit again.

Squatness answered 6/7, 2017 at 8:1 Comment(0)
V
4

It's just a function ...

ngOnInit() {}

secondMethod() { this.ngOnInit(); }

I've been doing it all the time to reload my data, never had a problem with it.

Vitascope answered 6/7, 2017 at 8:2 Comment(2)
you should not call ngOnInit life cycle from another custom functionHoad
@Mdomerarafat Could you explain why not?Haem

© 2022 - 2024 — McMap. All rights reserved.