Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run function when an ionic 2 page has fully loaded

Is there a way to determine when an ionic 2 page has fully loaded? I need to insert some html into the page when all of the page html has been loaded. Ideally I would like to have a function in my @page class that runs when the page has been loaded.

At the moment I am inserting an iframe containing a vine into a div container. I am experiencing, when running the app on an iPhone 5 running iOS 9, some peculiar behaviour where the same vine sometimes sizes to the container and sometimes doesn't. I am guessing that this might be due to timing (the source for the vine iframe is received as part of an http request).

like image 914
Bill Noble Avatar asked Apr 10 '16 18:04

Bill Noble


People also ask

How do you execute a function when a page is fully loaded?

Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded.

How to execute a function when page has fully loaded in Angular?

We can set the window. onload method to a function we want to run code when the page is fully loaded. This is because window. onload runs when the page is fully loaded.

How do I know if angular is fully loaded?

map(url => false); // Iterate over the images urls. forEach((url, index) => { // Create an HTML image let img = new Image(); // Listen to its loading event img. onload = () => { // Image has loaded, save the information this. haveImagesLoaded[index] = true; // If all images have loaded, set your loader to false this.

How do I use ionViewWillEnter?

ionViewWillEnter : It's fired when entering a page, before it becomes the active one. Use it for tasks you want to do every time you enter in the view (setting event listeners, updating a table, etc.). ionViewDidEnter : Fired when entering a page, after it becomes the active page. Quite similar to the previous one.


1 Answers

You can use the ionViewDidLoad method:

@Page({
  templateUrl: 'build/pages/somepage/somepage.html'
})
export class SomePage {
  constructor() {
    // ...
  }

  ionViewDidLoad() {
    // Put here the code you want to execute
  }
}

The lifecycle events as November 18, 2016 are:

  • ionViewDidLoad
  • ionViewWillEnter
  • ionViewDidEnter
  • ionViewWillLeave
  • ionViewDidLeave
  • ionViewWillUnload
  • ionViewCanEnter

Since Ionic 2 is in active development, things change all the time. If you would like to check the current lifecycle events, please refer to: https://ionicframework.com/docs/v2/api/navigation/NavController/#lifecycle-events

like image 137
artberri Avatar answered Oct 06 '22 23:10

artberri