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 ?
- JQuery | Set focus on a form input text field on page load
- How to load jQuery code after loading the page?
- What Happens To CSS When We Load a Page?
- How to get focused a button automatically when the page load using HTML ?
- How to Load External JS Scripts Dynamically in AngularJS ?
- How to display search result of another page on same page using ajax in JSP?
- How to show Page Loading div until the page has finished loading?
- How to redirect a page to another page in HTML ?
- How to pass form variables from one page to other page in PHP ?
- How to reload or re-render the entire page using AngularJS?
- What is SPA (Single page application) in AngularJS?
- Responsive page in AngularJS
- Model-View-Controller(MVC) architecture for Node applications
- HTML | DOM Audio controller Property
- Laravel | Route::resource vs Route::controller
- Laravel | Controller Basics
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.