The task is 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.
<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.
Example 2: In this example we will assign an object to the variable gfg and use it.
Output: The variable “gfg” is initialized successfully.
Example 3: In this example we will directly initialize a variable from ng-init directive.
Output: The variable gfg is assigned the value “GeeksForGeeks” on page load.
- AngularJS | ng-controller Directive
- How to insert HTML into view from AngularJS controller?
- How to create fade-in effect on page load using CSS ?
- How to load jQuery code after loading the page?
- JQuery | Set focus on a form input text field on page load
- PHP | DOMDocument load() Function
- How to display search result of another page on same page using ajax in JSP?
- How to pass form variables from one page to other page in PHP ?
- How to add API function to a simple PHP Page ?
- AngularJS | angular.forEach() Function
- AngularJS | angular.bootstrap() Function
- AngularJS | angular.uppercase() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.