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 send REST response to html in Angular ?
Next article icon

How to reuse template HTML block in Angular ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Code Reusability is the capacity to repurpose pre-existing code when developing new software applications. It will allow you to store a piece of code that does a single task inside a defined block, and then call that code whenever you need it. In this article, we will learn How to reuse template HTML blocks in Angular.

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 create a reuse a <h2> tag

HTML
<!-- app.component.html --> <ng-template #MsgRef>     <h2 style="color: red">GeeksforGeeks</h2>     <br> </ng-template> <h2 style="color: green;">GeeksforGeeks</h2> <h2>How to reuse template HTML block in Angular ?</h2> <ng-template [ngTemplateOutlet]="MsgRef"></ng-template> <ng-template [ngTemplateOutlet]="MsgRef"></ng-template> <ng-template [ngTemplateOutlet]="MsgRef"></ng-template> 
JavaScript
// app.component.ts import { Component, OnInit } from '@angular/core';  @Component({     selector: 'app-root',     templateUrl: "./app.component.html",     styleUrls: ['./app.component.css'] })  export class AppComponent { } 
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-11-12-at-003916

Example 2: In this example, we will reuse an image template.

HTML
<!-- app.component.html --> <ng-template #MsgRef>     <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20231009182919/Angular-PrimeNG-copy.webp">     <br> </ng-template> <h2 style="color: green;">GeeksforGeeks</h2> <h2>How to reuse template HTML block in Angular ?</h2> <ng-template [ngTemplateOutlet]="MsgRef"></ng-template>  <ng-template [ngTemplateOutlet]="MsgRef"></ng-template> <ng-template [ngTemplateOutlet]="MsgRef"></ng-template> 
JavaScript
// app.component.ts import { Component, OnInit } from '@angular/core';  @Component({     selector: 'app-root',     templateUrl: "./app.component.html",     styleUrls: ['./app.component.css'] })  export class AppComponent { } 
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:


Next Article
How to send REST response to html in Angular ?

A

abhiisaxena09
Improve
Article Tags :
  • Web Technologies
  • AngularJS
  • Geeks Premier League
  • AngularJS-Questions
  • Geeks Premier League 2023

Similar Reads

  • How to send REST response to html in Angular ?
    In this article, we will see how to send API responses using HttpClient Module to an HTML using Angular, along with understanding the basic implementation through the examples. Angular is a JavaScript framework through which we can create reactive single-page web applications. For implementation, we
    3 min read
  • Angular PrimeNG BlockUI Templates
    Angular PrimeNG is an open-source framework with a rich set 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 know how to use Angular PrimeNG BlockUI Templates BlockUI component is used
    3 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 use Handlebars to Render HTML Templates ?
    Handlebars is a powerful template engine that allows us to create dynamic HTML templates. It helps to render HTML templates easily. One of the advantages of Handlebars is its simplicity, flexibility, and compatibility with various web development frameworks. These are the following methods: Table of
    5 min read
  • Introduction To Components And Templates in Angular
    Angular is a powerful framework for building dynamic, single-page web applications. One of the core concepts that make Angular so effective is its use of components and templates. Components and templates are the building blocks of any Angular application, allowing developers to create reusable, mai
    6 min read
  • Angular PrimeNG PickList Templates
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more.
    4 min read
  • AngularJS ng-bind-template Directive
    The ng-bind-template Directive in AngularJS is used to replace the content of an HTML element with the value of the given expression. It is used to bind more than one expression. It can have multiple {{ }} expressions. It is supported by all HTML elements. Syntax: The ng-bind-template Directive can
    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
  • Angular PrimeNG Form Chips Templates
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Angular PrimeNG Form Chips Templates Component. The Chips compo
    3 min read
  • Angular PrimeNG Form Editor Templates
    Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Angular PrimeNG Form Editor Templates. The Form Editor is a Qui
    3 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