Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a function once the page is fully loaded in AngularJS

Tags:

I would like to execute this function as soon as the page is fully loaded.

vm.pointInvoice = () => {   
    if ($stateParams.property == "INVOICE") {
        vm.invoiceEdit($stateParams.checkin)
        vm.invoiceFocus = true;
        vm.receiptFocus = false;
    }
}

If I put the function as a button (just to test it), everything works perfectly

<button ng-click="vm.pointInvoice()">OPEN AND POINT TO INVOICE</button>

But, no matter what I do - I simply cannot get this to execute my function automatically (when the page is fully loaded and all data/elements are available).

Lucky me Stack Overflow had a lot of posts about page fully loaded so I tried a whole bunch of them but none of them works, they all fire off the function while the page is still blank.

These are some of the ones I have tried:

$scope.$on('$viewContentLoaded', function(){
    debugger;
});

angular.element(document).ready(function () {
    debugger;
});

$scope.$on('$stateChangeSuccess', function () {
    debugger;
});

So my only idea left is to do some ugly setTimeout(function(){ vm.pointInvoice() }, 3000); hack but to me thats a bit like giving up :-D

There MUST be some way to fire off an function when the page is fully loaded....

like image 434
torbenrudgaard Avatar asked Aug 30 '17 11:08

torbenrudgaard


People also ask

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 you call a function in AngularJS?

You can call your angularJS function from javascript or jquery with the help of angular. element().

What does $compile do in AngularJS?

Overview. Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together. The compilation is a process of walking the DOM tree and matching DOM elements to directives. Note: This document is an in-depth reference of all directive options.

What is Digest function in Angular?

digest() function will iterates through the watch list variables and check if any changes made for that variables or not. In case if any changes found for that watch list variables immediately corresponding event listener function will call and update respective variable values with new value in view of application.


1 Answers

You may want to use the ng-init directive:

<div ng-init="init()">...</div>

Define this function is your controller:

$scope.init = function() {
    alert("Page is fully loaded!);
}

Anyway, you can call this function directly from your controller. It will be call once the app and the controller are loaded.

like image 132
Mistalis Avatar answered Oct 04 '22 20:10

Mistalis