The Wayback Machine - https://web.archive.org/web/20240617215542/https://www.geeksforgeeks.org/how-to-display-variable-from-ts-file-to-html-file-in-angular-17/
Open In App

How to display variable from .ts file to .html file in angular 17

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Angular, when you’re working on a component, you use TypeScript files (.ts) to write the code that controls how the component behaves. On the other hand, HTML files (.html) handle how the user interface looks and gets displayed.

If you want to show a variable from the TypeScript file in the HTML file, you have to use something called data binding. Data binding basically connects the properties of your component to what gets shown on the screen, making sure everything stays in sync.

Data Binding

Data binding is a fundamental concept in Angular that allows you to pass data between the component and the view. There are four different types of data binding in Angular:

  • Interpolation: Allows you to display component properties in the HTML template using the double curly brace syntax {{ }}.
  • Property Binding: Allows you to bind component properties to HTML element properties.

In this article, we’ll focus on interpolation, which is the most common way to display variables from the .ts file in the .html file.

Steps to Create Angular Application

Follow these steps to create an Angular application and implement the approaches mentioned above:

Step 1: Create a new Angular project

Open your terminal or command prompt and run the following command to create a new Angular project:

ng new my-app

Folder Structure:Screenshot-2024-04-15-115841

Dependencies

"dependencies": {
"@angular/animations": "^17.2.0",
"@angular/common": "^17.2.0",
"@angular/compiler": "^17.2.0",
"@angular/core": "^17.2.0",
"@angular/forms": "^17.2.0",
"@angular/platform-browser": "^17.2.0",
"@angular/platform-browser-dynamic": "^17.2.0",
"@angular/platform-server": "^17.2.0",
"@angular/router": "^17.2.0",
"@angular/ssr": "^17.2.3",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}

Approach 1: Interpolation

To display a variable from the .ts file in the .html file using interpolation, you need to wrap the variable name with double curly braces {{ }}.

Syntax:

<p>{{ variableName }}</p>

Code Example:

HTML
<!-- app.component.html -->

<h1>{{ title }}</h1>
<p>Hello, {{ name }}!</p>
JavaScript
//app.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'Angular Data Binding';
    name = 'John Doe';
}

To start the application run the following command.

ng serve

Output:

Angular Interpolation example

Ouptut

Approach 2: Property Binding

You can also use property binding to display variables in the HTML template. This approach is useful when you want to bind the variable to an element property, such as src for an image or value for an input field.

Syntax:

<img [src]="imageUrl" />
<input [value]="inputValue" />

Code Example:

HTML
<!-- app.component.html -->

<h1>{{ title }}</h1>
<p>Hello, {{ name }}!</p>
<img [src]="imageUrl" alt="Example Image">
JavaScript
// app.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'Angular Data Binding';
    name = 'John Doe';
    imageUrl = 'https://example.com/image.jpg';
}

Output:

Property Binding Angular

Output



Previous Article
Next Article

Similar Reads

What is the difference between display: inline and display: inline-block in CSS?
The display property in CSS is a very useful and commonly used property that contains many values. The display property defines how an HTML element should be displayed on the webpage. The property also specifies the type of box used for an HTML element and how it should be laid out on the page. If we need to display the elements that are laid out a
4 min read
How display: inline is different from display: inline-block in CSS ?
In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline &amp; display: inline-block properties, &amp; will understand their basic differences &amp; implementation through the examples. The display property facilitates setting the element by
3 min read
What is the difference between display:inline-flex and display:flex in CSS ?
In this article, we will see the display property &amp; its values, i.e. the inline-flex &amp; flex, in order to specify the display behavior of an element, along with understanding their basic differences &amp; will illustrate them through the examples. Both CSS display: inline-flex and display: flex properties are used to create flexible layouts.
3 min read
How to Display JavaScript Variable Value in Alert Box ?
JavaScript is a powerful scripting language widely used in web development to enhance user interactivity and functionality. Often, developers need to display variable values to users, either for debugging purposes or to provide informative messages. One common method to achieve this is by using an alert box. Below are the approaches to display Java
3 min read
How to Display JavaScript Variable Value in Alert Box ?
An alert box is a dialog box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notifications to users. ApproachFirst, create your HTML and C
2 min read
variable === undefined vs. typeof variable === “undefined” in JavaScript
Undefined comes into the picture when any variable is defined already but not has been assigned any value. Undefined is not a keyword. A function can also be undefined when it doesn't have the value returned. There are two ways to determine if a variable is not defined, Value and type. var geeks; alert ( geeks === undefined) It's so clear that you
2 min read
How to fetch data from JSON file and display in HTML table using jQuery ?
The task is to fetch data from the given JSON file and convert data into an HTML table. Approach: We have a JSON file containing data in the form of an array of objects. In our code, we are using jQuery to complete our task. The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two
3 min read
How to pass/access node.js variable to an html file/template ?
The following approach covers how to pass/access node.js variable to an HTML file/template. We will build a basic Node.js application that will let you show dynamic content on an HTML page, i.e. your name and age. For adding the name and age values to our HTML template, we'll be using the pug package. Pug is a template engine that can be used to in
2 min read
How to display HTML tags as plain text in HTML ?
In this article, we will learn how to display HTML tags as plain text in HTML. We can replace the plain text by using &lt; with   &amp;lt; or &amp;60; and &gt;   with  &amp;gt; or &amp;62; on each HTML tag. HTML Entities:Sign DescriptionEntity nameEntity number&lt;Less than(start of HTML element)&amp;lt;&lt;&gt;Greater than(end of HTML element)
2 min read
Angular PrimeNG Form TreeSelect Chips Display Component
Angular PrimeNG is an open-source library that consists of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will be seeing the Angular PrimeNG Form TreeSelect Chips Display Component. Form TreeSelect Chips Display: The items that have been se
4 min read