Difference between views and templateUrl in AngularJS
Last Updated :
08 Nov, 2022
In this article, we will see what is Views and TemplateUrl in AngularJS, along with understanding their basic usage through the code snippets & finally see some differences between them.
A Views is anything that a user sees on a screen and is used to set up multiple views or to target views manually. Angular uses views to connect with the platform. In Angular for each element, there is a view corresponding to that element.
Types of Views: The different types of Views are described below:
1. View Containers: View Container holds host as well as embedded views. And these are commonly referred to as views. Each @Component class registers a view container with Angular. Another type of reference for view container is ViewContainerRef it represents a container where one or more views can be connected.
Example: This example explains the basic use of the View Container in AngularJS.
JavaScript
@Component({
selector: "sample",
template:
`<h2>View Container</h2>
<ng-container #vc></ng-container>
<p>This is an example of view container</p>`,
})
export class SampleComponent implements AfterViewInit {
@ViewChild("vc", { read: ViewContainerRef }) vc: ViewContainerRef;
ngAfterViewInit(): void {
// outputs `template bindings={}`
console.log(this.vc.element.nativeElement.textContent);
}
}
Output:
2. Host Views: This type of view hosts dynamic components. Host views are generated by the Angular compiler for every component which is specified in either bootstrap or entryComponent module. Now each host view is responsible for generating a component view when factory.createComponent is called. Host views are attached with DOM elements.
Example: In this example, for any host view to work, a view container that holds views must exist. We have two view containers present here <app-example></app-example> and <ng-container></ng-container>.
JavaScript
import { Component } from "@angular/core";
@Component({
template: `
<h1>Host View</h1>
<h3>Dynamically Generated!</h3>
`,
})
export class AnotherComponent {}
JavaScript
import { AfterViewInit, Component, ViewChild,
ViewContainerRef,
ComponentFactoryResolver } from "@angular/core";
import { AnotherComponent } from "./another.component";
@Component({
selector: "app-example",
template: `
<h1>Application Content</h1>
<ng-container #container></ng-container>
<h3>Application using host views</h3>`,
entryComponents: [AnotherComponent],
})
export class ExampleComponent implements AfterViewInit {
@ViewChild("container", { read: ViewContainerRef }) ctr: ViewContainerRef;
constructor(private resolve: ComponentFactoryResolver) {}
ngAfterViewInit() {
const factory = this.resolve.resolveComponentFactory(AnotherComponent);
this.ctr.createComponent(factory);
}
}
Output:

3. Embedded Views: These types of views connect with other views with no added input. Basically, these views are created for view nodes specified in the ng-template. Unlike host views embedded views attach to other views.
The main difference between embedded and host views is that embedded views are particularly linked to a template whereas host views are linked to components.
Example: This example explains the basic use of Embedded Views in AngularJS.
JavaScript
import { Component, AfterViewInit, ViewChild,
ViewContainerRef,
TemplateRef } from "@angular/core";
@Component({
selector: "app-example",
template: `
<h1>Embedded View Example</h1>
<ng-container #container></ng-container>
<!-- embed view here -->
<h3>This is an example of embedded view</h3>
<ng-template #template>
<h1>This view is linked with template</h1>
<h3>Dynamically Generated!</h3>
</ng-template>`,
})
export class ExampleComponent implements AfterViewInit {
@ViewChild("template", { read: TemplateRef }) tpl: TemplateRef<any>;
@ViewChild("container", { read: ViewContainerRef }) ctr: ViewContainerRef;
constructor() {}
ngAfterViewInit() {
const view = this.tpl.createEmbeddedView(null);
this.ctr.insert(view);
}
}
Output:

TemplateUrl: It is a property in Angular Component Decorator. External Template uses templateUrl property to access the HTML file which is defined in a separate location, and the templateUrl does the work of taking the path of that file. Let's understand templateUrl with an example.
Example: This example explains the basic use of the TemplateUrl in AngularJS
app.component.ts:
JavaScript
@Component({
selector: "email-list",
templateUrl: "./email-list.component.html",
providers: [EmailService],
})
export class EmailListComponent implements OnInit {
title = "app";
}
app.component.html:
HTML
<!DOCTYPE html>
<html>
<body>
<h2>Email List File</h2>
<p>
This is a HTML file used by external
template and is accessed using
templateUrl property
</p>
</body>
</html>
Output:

Difference between Views & TamplateUrl:
Views | TemplateUrl |
---|
Anything that a user sees on the screen.
| A property used by external templates.
|
Used for creating Single Page Applications.
| Used to fetch the path of the external HTML file.
|
Used to set up multiple views.
| Does not set up multiple views.
|
It doesn't take file references.
| It takes file references.
|
Similar Reads
Difference between Template and TemplateURL in AngularJS As we know that, the @Component decorator is a function that accepts one object and that object has many properties. The major two important properties are template & template URL. There are various Method to create Templates in Angular The template is a part of a component that is used for the
5 min read
Difference Between Template And TemplateUrl In Angular Occasionally, you may want to define a component's template in Angular to control how it appears and functions. Angular offers two methods to add a template to a component: template and templateUrl. Despite having a similar appearance, they have diverse functions.When using Angular, the @Component d
5 min read
Difference between VueJS and AngularJS In this article, we will see what is AngularJS & VueJS, along with knowing their different features, and basic usage with the help of illustrations. In the last, we will see the relevant differences between them. AngularJS is an open-source front-end structural framework for making dynamic singl
4 min read
What Is Difference Between Style And StyleUrl In Angular? When you create a component in Angular, you sometimes want to style it to look good and match your application's design. Angular provides two ways to add styles to a component: style and styleUrls. They might look similar but they have different purposes. We know that the decorator functions of @Com
5 min read
Difference between ng-container and ng-template in AngularJS Both ng-container and ng-template can be used to create responsive and dynamic components. Angular provides a set of structural directives that can be used with both ng-template and ng-container such as: ng-ifng-forng-switch. These structural directives are used to alter the structure of the DOM by
3 min read
Difference between link and compile in AngularJS In this article, we will see the link and compile features in Angular JS, along with understanding their implementation through the illustration and exploring the key differences between them. One of the fundamental components of AngularJS is the directive. When creating web components, a directive
5 min read
Difference between Node.js and AngularJS Node.js is a runtime environment for executing JavaScript on the server-side, enabling backend development. AngularJS is a front-end framework for building dynamic, single-page web applications using MVC architecture on the client-side. Table of Content AngularJSNodeJSDifference between AngularJS an
4 min read
Difference between JavaScript and AngularJS In this article, we will see what are JavaScript & AngularJS and its features along with the understanding of their basic implementation. Finally, will discuss the differences between them. JavaScript is a lightweight and object-oriented scripting language used to create dynamic HTML pages with
4 min read
What is the Difference Between $routeProvider and $stateProvider in AngularJS ? In AngularJS, as we create Single-Page Applications, we need to implement the routing of components to view those images without having full page reloads. This can be achieved with the most popular routing methods, i.e., $routeProvider (ngRoute) and $stateProvider (ui-router).In this article, we wil
5 min read
What is the Difference Between $evalAsync and $timeout in AngularJS? AngularJS is a JavaScript framework, which may be utilized by including it in an HTML web page the usage of a <script> tag. AngularJS enables in extension the HTML attributes with the assistance of directives and binding of data to the HTML with expressions. It provides various tools for handl
5 min read