VOOZH about

URL: https://www.geeksforgeeks.org/angular-js/how-to-execute-angularjs-controller-function-on-page-load/

⇱ How to execute AngularJS controller function on page load ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to execute AngularJS controller function on page load ?

Last Updated : 8 Sep, 2022

In this article, we will see how to execute/call a JS function on page load using AngularJS. This function can be used to perform initialization. Calling a function or initializing a single value on page load in AngularJS is quite easy. AngularJS provides us with a dedicated directive for this specific task. It's the ng-init directive. 

 

Syntax:

<element ng-init="function()"> 
 Contents... 
</element>

Example 1: In this example, we will call a function to initialize a variable on page load. 

Output: The function is called on page load and the value of variable gfg is set to GeeksForGeeks. 👁 Function Call on Page Load AngularJS output

Example 2: In this example, we will assign an object to the variable gfg and use it. 

Output: The variable "gfg" is initialized successfully.

👁 GFG as an object output
 

Example 3: In this example, we will directly initialize a variable from the ng-init directive. 

Output: The variable gfg is assigned the value "GeeksForGeeks" on page load. 👁 init value on Page Load AngularJS output

Comment

Explore