AngularJS – How to call function on page load example with demo?

AngularJS – How to call function on page load example with demo?

In this post we will give you information about AngularJS – How to call function on page load example with demo?. Hear we will give you detail about AngularJS – How to call function on page load example with demo?And how to use it also give you demo for it if it is necessary.

Today, I am going to show you how to call controller function when page will load in AngularJS. We sometimes require to run function when page load like for example if you need to check user is login or not in AngularJS, It is possible by fire one Ajax request When Page Load.

There are several way to you can do this. I have two example for execute function on page load. In this example i give you very simple example, so you can simply understand and use it properly.

First Example using “data-ng-init” attribute and Second example “$window.onload” function.

So, Let’s check both example and try it…

Example 1:

<!DOCTYPE html>

<html>

<head>

<title>Angularjs call function on page load</title>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>

</head>

<body>


<div ng-app="myApp" ng-controller="mainController" data-ng-init="init()">

<h2>Angularjs call function on page load</h2>

</div>


<script type="text/javascript">

var myApp = angular.module("myApp", []);


myApp.controller("mainController", function($scope, $window) {


$scope.init = function () {

alert("Angularjs call function on page load");

};


});


</script>


</body>

</html>

Example 2:

<!DOCTYPE html>

<html>

<head>

<title>Angularjs call function on page load</title>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>

</head>

<body>


<div ng-app="myApp" ng-controller="mainController">

<h2>Angularjs call function on page load</h2>

</div>


<script type="text/javascript">

var myApp = angular.module("myApp", []);


myApp.controller("mainController", function($scope, $window) {


$window.onload = function() {

alert("Angularjs call function on page load");

};


});


</script>


</body>

</html>

Hope this code and post will helped you for implement AngularJS – How to call function on page load example with demo?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  7  =  

We accept paid advance and paid guest Posting on our Site : Contact US