Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • AngularJS Tutorial
  • AngularJS Directives
  • AngularJS Functions
  • AngularJS Filters
  • AngularJS Examples
  • AngularJS Interview Questions
  • Angular ngx Bootstrap
  • AngularJS Cheat Sheet
  • AngularJS PrimeNG
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to create nested controllers in Angular.js ?
Next article icon

How to execute AngularJS controller function on page load ?

Last Updated : 08 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

HTML
<!DOCTYPE html> <html ng-app="myApp">  <head>     <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js">     </script> </head>  <body ng-controller="MyController">        <!-- calling the firstFunction to         initialize gfg variable -->     <center ng-init="firstFunction(this)">                <!-- gfg variable with no value initially -->         <h1 style="color: green;">{{gfg}}</h1>     </center> </body>  <script type="text/javascript">     var myApp = angular.module('myApp', []);     myApp.controller('MyController',                       ['$scope', function($scope) {              // Function to be called on page load         $scope.firstFunction = function($scope) {                        // We need $scope argument as we are             // altering the variables defined in             // the $scope             $scope.gfg = "GeeksForGeeks"         }     }]); </script> </html> 

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. 

HTML
<!DOCTYPE html> <html ng-app="myApp">  <head>     <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js">     </script> </head>  <body ng-controller="MyController">      <!-- Calling the firstFunction to         initialize gfg variable -->     <center ng-init="firstFunction(this)">          <!-- gfg variable as an object -->         <h1 style="color: green;">{{gfg.name}}</h1>         <h3 style="color: green;">{{gfg.location}}</h3>     </center> </body>  <script type="text/javascript">     var myApp = angular.module('myApp', []);     myApp.controller('MyController', ['$scope',      function($scope) {              // Function to be called on page load         $scope.firstFunction = function($scope) {                  // We need $scope argument as we are             // altering the variables defined in             // the $scope                  // Assigning an object to the gfg variable             $scope.gfg = {                 name: "GeeksForGeeks",                 location: "India"             }         }     }]); </script> </html> 

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. 

HTML
<!DOCTYPE html> <html ng-app="myApp">  <head>     <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js">     </script> </head>  <body ng-controller="MyController">      <!-- initializing the gfg variable to         'GeeksForGeeks' -->     <center ng-init="gfg='GeeksForGeeks'">         <h1 style="color: green;">{{gfg}}</h1>     </center> </body>  <script type="text/javascript">     var myApp = angular.module('myApp', []);     myApp.controller('MyController',                       ['$scope', function($scope) {          }]); </script>  </html> 

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


Next Article
How to create nested controllers in Angular.js ?

T

thvardhan
Improve
Article Tags :
  • Web Technologies
  • AngularJS
  • AngularJS-Questions

Similar Reads

  • How to Call a Vue.js Function on Page Load ?
    In this article, we will learn how to call a vue.js function on page load in VueJS. We will explore two different approaches with practical implementation in terms of examples and outputs. Table of Content Using mounted HookUsing created HookUsing mounted HookIn this approach, are using the mounted
    2 min read
  • How to call an AngularJS Function inside HTML ?
    A Function is a set of statements that takes input, does some specific computation, and produces output. In this article, we will learn How to Call an AngularJS function inside HTML. To achieve this, we can use {{...}} to call the function from HTML. We can also pass arguments and return the result
    3 min read
  • How to call a function on click event in Angular2 ?
    A Function is a set of statements that takes input, does some specific computation, and produces output. An on click event occurs when the user clicks on an element. In this article, we will see How to call a function on click event in Angular2, along with understanding the basic implementation thro
    3 min read
  • How to create nested controllers in Angular.js ?
    A controller in AngularJS is a JavaScript object created with the help of a JavaScript object constructor. A controller can contain properties and functions. Controllers are used for controlling the application data of an AngularJS application. In this article, we will see the nested controllers in
    4 min read
  • How to use filter within controllers in AngularJS ?
    In this article, we will see how to use the Filter inside the controller using AngularJS. Filters are used to format the value of an expression and display it to the user. It can be used in HTML Previews, Controllers or Services, and directives. AngularJS facilitates many built-in filters, although,
    4 min read
  • How to share data between controllers in AngularJS ?
    The task is to share data variables between two or more controllers by using AngularJS. There are many procedures to achieve this. Here we will discuss the most popular ones. Approach: To share data between the controllers in AngularJS we have two main cases: Share data between parent and child: Her
    3 min read
  • How to insert HTML into view from AngularJS controller?
    The ng-bind-html directive is a secure way of binding content to an HTML element. So in order to insert HTML into view, we use the respective directive. While using AngularJS, write HTML in our corresponding application, we should check the HTML for dangerous or error prone code. By including the "a
    2 min read
  • AngularJS ng-controller Directive
    The ng-controller Directive in AngularJS is used to add a controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions. Syntax: <element ng-controller="expression"> Contents... </element
    2 min read
  • How to execute a routing in the AngularJS framework ?
    In this article, we will learn about how we can perform routing in AngularJS, along with understanding its implementation through the code example.   Routing in AngularJS helps to develop Single Page Applications, which is a web app that loads only a single page, and the part of the page instead of
    7 min read
  • How to detect a route change in AngularJS ?
    In this article, we will see how to detect a route change in AngularJS. In order to detect route change at any moment in AngularJS, this can be achieved by using the $on() method. The $on() method is an event handler, the event which will handle $routeChangeSuccess which gets triggered when route/vi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences