2

I want a method get called automatically when template get completely loaded. I have tried lifecycle hooks of ionic as well as angular and I have also tried to call method from template. html

My HTML file contains the following:

<h1>{{ myMethod() }}</h1>

But this is calling the myMethod function many times.

22
  • What does myMethod do, and why is it not acceptable for this function to be called multiple times? Commented May 20, 2018 at 14:17
  • I want to load map on device when side menu button is clicked. That's why it is unacceptable to be called multiple times. Commented May 20, 2018 at 14:42
  • So myMethod loads the map? Shouldn't this then be called by the button's click event? You should assume bindings like this in the view will be called multiple times. Commented May 20, 2018 at 14:43
  • did you try ngAfterViewChecked? that's the last lifecycle hook which runs after components & all its children are fully rendered with DOM changes applied. Commented May 20, 2018 at 15:04
  • @David On clicking side menu button. It reset root page again. And load new page. Hence click event is not of much use. Commented May 20, 2018 at 15:05

2 Answers 2

1

Because you need to call myMethod() only one time, it is better to use ionViewDidLoad. Because this event fired only when a view is stored in memory. This event is NOT fired on entering a view that is already cached. you could find out more information from here.

So if you need to call the method (myMethod()) even after this event is fired it is better to use timeout inside this event to call your method. Give desire timeout that match with your scenario.

Sample code:

ionViewDidLoad() {
  setTimeout(() => {
    myMethod();
  }, 300);
}

Hope this will help to solve your problem.

Sign up to request clarification or add additional context in comments.

9 Comments

Doesn't it make unnecessary delay in the app.
but this is the only place match with your problem. try to adjust that delay according to your scenario in effective and efficient way. this is not unnecessary delay. because it is necessary to solve your problem. Also this delay effect to your app only one time.
I think of this earlier but I don't want to use it. Now I realised that there is no other way and have to use timeout only. Thanks
np :) if you find out another way to achieve this don't forget to update here.. ;)
sure. I will :)
|
0

You should go for ngAfterViewInit life cycle hook. ngAfterViewInit() is called after the view is initially rendered.

export class YourComponent implements AfterViewInit {    
  constructor() {    
  }

  ngAfterViewInit() {
    myMethod();
  }
}

2 Comments

I have tried lifecycle hook of angular as well as ionic. These methods are called before rendering template.
@TonyStark ok, did you try ionViewDidLoad() { // Put here the code you want to execute } ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.