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 call JavaScript function in HTML ?
Next article icon

How to call an AngularJS Function inside HTML ?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 back to HTML.

Steps for Installing & Configuring the Angular Application

Step 1: Create an Angular application using the following command.

ng new appname

Step 2:  After creating your project folder i.e. appname, move to it using the following command.

cd appname

Project Structure

It will look like the following:

z132

Example 1: In this example, we will call a function from HTML. This function is written in the ts file. We will call an alert() from this function.

HTML
<!-- app.component.html --> <h2 style="color: green">   GeeksforGeeks </h2> <h2>   How to Call an AngularJS function inside HTML </h2> <h3>Open your console</h3> {{displayMessage()}} 
JavaScript
// app.component.ts import { Component, OnInit } from '@angular/core'; import { KeyValue } from '@angular/common'; import { Pipe, PipeTransform } from '@angular/core';  @Component({     selector: 'app-root',     templateUrl: "./app.component.html",     styleUrls: ['./app.component.css'] })  export class AppComponent {     displayMessage() {         console.log(         "Hi GeeksforGeeks!! Happy Learning Angular!!")     } } 
JavaScript
// app.module.ts import { NgModule }      from '@angular/core'; import { BrowserModule }      from '@angular/platform-browser'; import { HttpClientModule }     from '@angular/common/http'; import { AppComponent }      from './app.component';  @NgModule({     declarations: [         AppComponent     ],     imports: [         BrowserModule,         HttpClientModule,     ],     providers: [],     bootstrap: [AppComponent] })  export class AppModule { } 

Output:

Recording-2023-10-27-at-011813

Example 2: In this example, we will see that we can pass arguments in the function, and the result will be returned by the function on the front page.

HTML
<!-- app.component.html --> <h2 style="color: green">   GeeksforGeeks </h2> <h2>   How to Call an AngularJS function inside HTML </h2> <h3 style="color:crimson">       {{displayMessage("Hi GeeksforGeeks!! Happy Learning Angular!!")}} </h3> <h3 style="color: darkorange;">   Sum of 2 and 3 is :  {{getSum(2,3)}} </h3> 
JavaScript
// app.component.ts import { Component, OnInit } from '@angular/core'; import { KeyValue } from '@angular/common'; import { Pipe, PipeTransform } from '@angular/core';  @Component({     selector: 'app-root',     templateUrl: "./app.component.html",     styleUrls: ['./app.component.css'] })  export class AppComponent {     displayMessage(msg: string) {         return msg;     }      getSum(a: number, b: number) {         return a + b     } } 
JavaScript
// app.module.ts import { NgModule }      from '@angular/core'; import { BrowserModule }      from '@angular/platform-browser'; import { HttpClientModule }      from '@angular/common/http'; import { AppComponent }     from './app.component';  @NgModule({     declarations: [         AppComponent     ],     imports: [         BrowserModule,         HttpClientModule,     ],     providers: [],     bootstrap: [AppComponent] })  export class AppModule { } 

Output:

Recording-2023-10-27-at-012332


Next Article
How to call JavaScript function in HTML ?

N

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

Similar Reads

  • How to make an http call in angular
    Angular, with its robust framework and extensive ecosystem, offers you powerful tools for building modern web applications. One such tool is the HttpClient module, which provides a seamless way to make HTTP calls and interact with RESTful APIs or backend services. In this article, we'll explore maki
    3 min read
  • How to call JavaScript function in HTML ?
    In HTML, you can easily call JavaScript functions using event attributes like onclick and onload. Just reference the function name within these attributes to trigger it. You can also call functions directly within script blocks using standard JavaScript syntax. Let's create an HTML structure with so
    2 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 execute AngularJS controller function on page load ?
    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 speci
    2 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
  • How to set, get and clear cookies in AngularJS ?
    In this article, we will see how to set, get & clear the Cookies in AngularJS, along with understanding the basic usage through the implementation. In AngularJS, we need to use angular-cookies.js to set, get and clear the cookies. You can use the live CDN link for this: https://cdnjs.cloudflare.
    2 min read
  • How to access the Scope from outside JS Function in AngularJS ?
    In the AngularJS application, we generally need to access the scope from the outside JS function. This can be done using the global scope object like $rootScope or the AngularJS services. Accessing the scope from the outside of JS functions is to enable the manipulation of data in the scope of the a
    4 min read
  • How to count array items in AngularJS ?
    Given an array and the task is to get the length of an array variable in AngularJS. For this, we will be using the .length() method to get the length of an array variable. Syntax: array.length();Example 1: In this example, the array length is shown by the alert box. Here, we have an array containing
    2 min read
  • How to Enable HTML 5 Mode in Angular 1.x ?
    HTML5 mode in Angular1.x is the configuration that replaces the default hash-based URLs and provides more user-friendly URLs using the HTML5 History API. This feature mainly enhances our one-page application and also provides a better experience to the users. We need to configure our server with pro
    6 min read
  • How to hide an HTML element via a button click in AngularJS ?
    In this article, we will see how to use a toggle button feature to hide and display an element by button click in Angular. The toggle button is a user interface control that may be useful for situations when the user wants to switch between 2 states or conditions. For instance, in our smartphone, we
    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