Skip to main content

angular 11 tutorial for Beginners, Template and TemplateUrl in Angular,t...

                 

template VS templateUrl In Angular


Introduction

 
This article will explain the difference between template and templateUrl in angular. We will discuss these with examples.
  1. What is the templateUrl?
  2. What are the differences between the template and templateUrl Angular Components?
  3. When to use template over templateUrl and vice-versa?
  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.   template:'<h1>Hello, {{name}}</h1>',  
  6.   styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9.   title = 'DemoApplication';  
  10.   name='Ashish Mishra';  
  11. }  
In the above component, we used something called "template" to render the page, i.e., the user interface with which the end user can interact. Let’s discuss the different ways to create templates in Angular applications.
 

Different ways to create template in Angular

 
A template is a part of a component which is used to render the user interface in a web page. In Angular, we can create a template in two possible ways
  1. Inline template
  2. Template in an external file
In Angular applications, the inline templates are directly specified within the component using the template property. Below is an example of an Angular inline template.
  1. @Component({  
  2.   selector: 'app-root',  
  3.   template:'<h1>Hello, {{name}}</h1>',  
  4.   styleUrls: ['./app.component.css']  
  5. })  

Can’t we include the above HTML code within single or double quotes?

 
Yes, you can include the above HTML code within a pair of either single quotes or double quotes as long as your HTML code is in a single line as shown below. Here, we are using single quotes.
  1. @Component({  
  2.   selector: 'app-root',  
  3.   template:'<h1>Hello, {{name}}</h1>',  
  4.   styleUrls: ['./app.component.css']  
  5. })  
Replacing the above HTML Code with double quotes and it should work as expected.
  1. @Component({  
  2.   selector: 'app-root',  
  3.   template:"<h1>Hello, {{name}}</h1>",  
  4.   styleUrls: ['./app.component.css']  
  5. })  



Now, we are using inline template to render the view. But in real time, in most of the cases, you need to use the templateUrl. So, let us discuss the need and how to use templateUrl in an Angular application.
 

When to use templateUrl in Angular applications?

 
When you have a complex view, then it recommended by Angular to create that complex view in an external HTML file instead of an inline template. The Angular component decorator provides a property called templateUrl. This property takes the path of an external HTML file.
 
Step 1
 
Click on app folder under the "src" folder of application and open app.component.html.
  1. <div style="text-align:center">  
  2.   <h1>  
  3.     Hello,{{name}}.  
  4.     Welcome to {{ title }}!  
  5.   </h1>  
  6. </div>  
Step 2
 
Open the app.component.ts file. You will see the templateUrl property of the component decorator to the path of app.component.html.
  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.   templateUrl: './app.component.html',  
  6.   styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9.   name="Testy Codeiz";  
  10.   title = 'angular 11 programming.';  

Comments

Popular posts from this blog

#2 Angular 8 Tutorial in Hindi | Project Structure and File Structure in...

                     Angular 8 File Structure e2e(end-to-end) – This folder contains test cases for testing the complete application along with its specific configuration files. node_modules – Contains all the node-modules used in the angular application. src – Complete source code of the application resides in src folder. app – root component of the application. assets – contains all the images or resources used in the application. environments – contains build configuration environments of the application. We can create any target environment and specify its configuration here. index.html – This is the main html page which is rendered when someone opens your website or application. main.ts – This is the main entry point of the angular application which compiles the AppModule and renders the specified bootstrap component in the browser. styles.css – Contains all the styles to be used as globally in the application....

Build Realtime Chat Application using Angular 13.

in this video, we will see Angular chat application, Angular chat app, angular 13 chat application, realtime chat application with angular, real-time chat application,build a chat app with node and socket.io, build a chat app with angular and socket.io,what's app chat functionality,building a chat application with angular node js and socket.io Angular chat application with testycodeiz,angular chat project from scratch, angular project from scratch, angular 13 chat application with testycodeiz, angular chat app with testycodeiz, angular with testycodeiz, agular project with testycodeiz #chatapp, #angularchat #testycodeiz

Angular 11 CRUD Application Using MEAN Stack,angular CRUD Example, Angul...

              Angular 11 CRUD Application Using MEAN Stack, angular CRUD Example, Angular crud project,angular crud tutorial,angular crud operation,angular crud application,angular crud with mean stack, #Angular11 ​ , #CRUD ​ , #Angularproject ​ ,