How to run Angular function on document ready?

How to run AngularJS function once the document is ready. Till now we used most famous JAVASCRIPT framework name is “jQuery”. The jQuery is open source framework. We can write all the jQuery functions on “document ready” so once all document is loaded then after running the jQuery code.

You all know how we can write the document ready on jQuery like below

(function($){

    $(document).ready(function(){
        // Write the jQuery function here
        alert("Document Ready");
    })

})(jQuery);

Above you can see how we write the document ready method on jQuery. We can write same document ready on AngularJs. So the function is run after all the js file is loaded. let’s see how we can write AngularJs function on document ready.

angular.module('MyApp', [])
    .controller('MyCtrl', [function() {
        angular.element(document).ready(function () {
            // write javascript/jQuery code here
            document.getElementById('message').innerHTML = 'Document Ready';
        });
    }]);

We see how we can write the javascript/jquery code on document ready using AngularJs APP. it’s too easy to write the document ready on AngularJs.

If you face any issue after using this code please write comment below.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s