AngularJs Call Function on Page Load


AngularJs Call Function on Page Load – There are many ways to call a function on page load in angularjs. You can use $window object to call a function on page. Here in this tutorial we are going to explain how you can call function on page load in angularjs. We will explain this with example and demo.


AngularJs Call Function on Page Load

You can call a function on page load simply using the $window.onload as below –

AngularJs Call Function on Page Load: Example

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope, $window) {
$window.onload = function() {
 alert("Called on page load..");
};
});
</script> 
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
</div>  
</div> 

</body>  
</html>      
                                                                                                                                                                                                                                                                                                                                                                            

Try it »

The above function will be called on page load. The above function will trigger a alert on page load and it will produce output something like this –

AngularJs Call Function on Page Load


Advertisements

Add Comment

📖 Read More