ItSolutionStuff.com

How to Call Function on Page Load in AngularJS?

By Hardik Savani • October 20, 2023
jQuery Angular

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>

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

AngularJS Check Checkbox is Checked or Not Example

Read Now →

AngularJS Convert Comma Separated String to Array Example

Read Now →

AngularJS - How to Create Read More/Less Toggle using Directive?

Read Now →

How to Call AngularJS Controller Function in JQuery?

Read Now →

How to Remove # from URL in AngularJS?

Read Now →

How to Hide Div After Some Time in AngularJS?

Read Now →

AngularJS Pagination using dirPagination Directive Example

Read Now →

AngularJS Sorting(using Orderby Filter) Table Data Example

Read Now →

How to Change Date Format using Date Filter in AngularJS?

Read Now →

AngularJS - How to Limit String Length using Filter?

Read Now →

AngularJS - How to Capitalize the First Letter of Word Example

Read Now →

How to Remove HTML Tags from String in AngularJS?

Read Now →

PHP AngularJS CRUD with Search and Pagination Tutorial

Read Now →