(Go: >> BACK << -|- >> HOME <<)

SlideShare a Scribd company logo
ANGULAR DIRECTIVES
(UNIT 3)
Dr. P. Rambabu, M. Tech., Ph.D., F.I.E.
30-May-2022
Unit 3: Directives
Built-in Directives:
• How to use Built-in Directives:
• Types:
 Component Directives
 Structural Directives
 Attribute Directives.
Custom Directives:
• Creating a Custom Attribute Directive
• Creating a Custom Directive with a Component
Angular Directives
Angular Directives
Angular Directives
Built-in Directives
Directives extend the behavior of HTML, enabling you to create custom HTML elements,
attributes, and classes with functionality specific to an application. Angular provides many
built-in directives, which provide the capability to interact with form elements, bind data in a
component to the view, and interact with browser events.
Using Built-in Directives
Much of the Angular functionality that you need to implement in HTML elements is provided
through built-in directives. These directives provide a wide variety of support for Angular
applications. The following sections describe most of the Angular directives, which fall into
three categories:
1. Component: A directive with a template
2. Structural: A directive that manipulates elements in the DOM
3. Attribute: A directive that manipulates the appearance and behavior of a DOM element
Components Directives
Directives in Angular is a .js class, which is declared as @directive. We have 3 directives in
Angular.
A @Component decorator is actually a @Directive decorator extended with template-
oriented features. Whenever Angular renders a directive, it changes the DOM according to
the instructions given by the directive. Directive appears within an element tag similar to
attributes.
Structural Directives
Structural directives alter layout by adding, removing, and replacing elements in DOM.
*ngIf
When this directive is present in an element, that element is added to the DOM if the value
returns true. If the value returns false, then that element is removed from the DOM,
preventing that element from using resources. Here is an example:
<div *ngIf="students.length > 0">
…..
</div
*ngFor
This directive is used to create a copy of a template for each item within an iterable object.
Here is an example:
<div *ngFor="let student of students">
<div>Name: {{student.name}} </div>
</div>
*ngIf
Syntax:
<p *ngIf="condition"> content to render, when the condition is true </p>
app.component.ts
contacts:any = [
{idno:"101",name: "Sravani", email:"sravani@gmail.com"},
{idno:"102",name: "Abhinandan", email:"abhinandan@gmail.com"},
{idno:"103",name: "Maruti", email:"maruti@gmail.com"},
{idno:"104",name: "Kamalesh", email:"kamalesh@gmail.com"}
];
app.component.html
<div *ngIf="contacts.length>0">
<h5 class="text-center text-success">Some Content is there to display</h5>
</div>
*ngIf with else condition:
Syntax:
<div *ngIf="condition; then normal else elseBlock"></div>
<ng-template #normal>
Content to render when condition is true.
</ng-template>
<ng-template #elseBlock>
Content to render when condition is false.
</ng-template>
app.component.ts:
contacts:any = [];
app.component.ts:
<div *ngIf=" contacts.length >0; then fullContent else emptycontent">here is ignored</div>
<ng-template #fullContent>
<h3 class="text-center text-success">Some content is thre in declared variable</h3>
</ng-template>
<ng-template #emptycontent>
<h3 class="text-center text-danger">provided variable dont have content</h3>
</ng-template>
*ngFor
This directive works like a loop.
app.component.ts:
contacts:any = [
{idno:"101",name: "Sravani", email:"sravani@gmail.com"},
{idno:"102",name: "Abhinandan", email:"abhinandan@gmail.com"},
{idno:"103",name: "Maruti", email:"maruti@gmail.com"},
{idno:"104",name: "Kamalesh", email:"kamalesh@gmail.com"}
];
app.component.html:
<table class="table">
<thead>
<th>Id No</th>
<th>Name</th>
<th>Email</th>
</thead>
<tbody>
<tr *ngFor="let contact of contacts">
<td> {{contact.idno}} </td>
<td> {{contact.name}}
</td>
<td> {{contact.email}} </td>
</tr>
</tbody>
</table>
ngSwitch
This directive displays a template based on the value passed in it. As with ngIf, if
the value does not match the case, the element is not created. Here is an example:
<div [ngSwitch]=“student">
<span *ngSwitchCase = “ ‘Mohan’ “ > Hello Mohan </span>
<span *ngSwitchCase = “ ‘Vijay’ “ > Hello Vijay </span>
<span *ngSwitchDefault > Hello Student </span>
</div>
The ngSwitch directive relies on two other directives to work:
ngSwitchCase and ngSwitchDefault. These directives are be explained below.
ngSwitchCase:
This directive evaluates the value it has stored against the value passed into
ngSwitch and determines whether the HTML template it is attached to should be
created.
ngSwitchDefault:
This directive creates the HTML template if all the above ngSwitchCase
expressions evaluate to false. This ensures that some HTML is generated no
Attribute Directives
Angular attribute directives modify how HTML elements look and behave. They are injected
straight into the HTML and dynamically modify how the user interacts with an HTML
segment. Attribute directives are so named because they often look like normal HTML
attributes.
ngModel
This directive watches a variable for changes and then updates display values based on
those changes. Consider these examples:
app.component.html:
<input type="text" name="data" [(ngModel)]="data" />
<p> {{ data }} </p>
app.component.ts:
ngStyle
This directive updates the styles of an HTML element.
app.component.html:
Example 1:
<div class="col-md-4" [ngStyle]="{'background-color':'green'}">Hello</div>
Example 2:
<div [ngStyle]="{'background-color’: country === 'UK' ? 'yellow' : 'red' }">
{{country}}</div>
app.component.ts:
country:any= 'UK’;
utput:
ngClass
It controls the appearance of elements by adding and removing CSS classes
dynamically.
app.component.html:
<div [ngClass]=“ {'text-primary': country === 'UK’}” > {{country}} </div>
app.component.ts:
country:any= 'UK’;
output:
UK
ngForm
This directive creates a form group and allows it to track the values and validation
within that form group. By using ngSubmit, you can pass the form data as an
object to the submission event. Here is an example:
app.component.html:
<form #formName="ngForm" (ngSubmit)="onSubmit(formName)"> </form
Custom Directives
Creating a custom directive is just like creating an Angular component. To create a custom
directive we have to replace @Component decorator with @Directive decorator.
So, let's get started with creating our first Custom Attribute directive. In this directive, we
are going to highlight the selected DOM element by setting an element’s background color.
Procedure:
Step 1: Create Custom Directive ‘appHighlight’
ng generate directive appHighlight
Step 2: Add below code in appHighlight.ts
Step 3: Add below code in app.component.html
<h1 appAppHighlight > Highlight Me !</h1>
import { Directive, ElementRef } from '@angular/core';
constructor(private eleRef: ElementRef) {}
ngOnInit() {
this.eleRef.nativeElement.style.backgroundColor = "green";
this.eleRef.nativeElement.style.color = "blue";
}
Dr. Rambabu Palaka
Professor
School of Engineering
Malla Reddy University, Hyderabad
Mobile: +91-9652665840
Email: drrambabu@mallareddyuniversity.ac.in
Reference:
Node.js, MongoDB and Angular Web Development
by Brad Dayley, Brenden Dayley, Caleb Daley

More Related Content

What's hot

Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
Rohit Gupta
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Angular 9
Angular 9 Angular 9
Angular 9
Raja Vishnu
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
Varun C M
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
Laurent Duveau
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
Duy Khanh
 
Angular 14.pptx
Angular 14.pptxAngular 14.pptx
Angular 14.pptx
MohaNedGhawar
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
Manish Shekhawat
 
Angular 8
Angular 8 Angular 8
Angular 8
Sunil OS
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
David Parsons
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
Christoffer Noring
 
Angular
AngularAngular
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
之宇 趙
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
Jadson Santos
 

What's hot (20)

Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
 
Angular 9
Angular 9 Angular 9
Angular 9
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular 14.pptx
Angular 14.pptxAngular 14.pptx
Angular 14.pptx
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Angular
AngularAngular
Angular
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
 

Similar to Angular Directives

AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
Ran Wahle
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
Ran Wahle
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
Anuradha Bandara
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
Ravindra K
 
Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
Adam Moore
 
Unit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptxUnit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptx
Malla Reddy University
 
mean stack
mean stackmean stack
mean stack
michaelaaron25322
 
Directives
DirectivesDirectives
Directives
Brajesh Yadav
 
Introduction to Angularjs : kishan kumar
Introduction to Angularjs : kishan kumarIntroduction to Angularjs : kishan kumar
Introduction to Angularjs : kishan kumar
Appfinz Technologies
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
Stéphane Bégaudeau
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
EPAM Systems
 
AngularJS
AngularJSAngularJS
Angular2
Angular2Angular2
Angular2
kunalkumar376
 
Introduction to Angular Js
Introduction to Angular JsIntroduction to Angular Js
Introduction to Angular Js
Professional Guru
 
GDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and WorkshopGDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and Workshop
Drew Morris
 
Introduction to single page application with angular js
Introduction to single page application with angular jsIntroduction to single page application with angular js
Introduction to single page application with angular js
Mindfire Solutions
 
Angular Directive.pptx
Angular Directive.pptxAngular Directive.pptx
Angular Directive.pptx
AshokKumar616995
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directives
Marios Fakiolas
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
Matus Szabo
 

Similar to Angular Directives (20)

AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
 
Unit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptxUnit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptx
 
mean stack
mean stackmean stack
mean stack
 
Directives
DirectivesDirectives
Directives
 
Introduction to Angularjs : kishan kumar
Introduction to Angularjs : kishan kumarIntroduction to Angularjs : kishan kumar
Introduction to Angularjs : kishan kumar
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
AngularJS
AngularJSAngularJS
AngularJS
 
Angular2
Angular2Angular2
Angular2
 
Introduction to Angular Js
Introduction to Angular JsIntroduction to Angular Js
Introduction to Angular Js
 
GDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and WorkshopGDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and Workshop
 
Introduction to single page application with angular js
Introduction to single page application with angular jsIntroduction to single page application with angular js
Introduction to single page application with angular js
 
Angular Directive.pptx
Angular Directive.pptxAngular Directive.pptx
Angular Directive.pptx
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directives
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
 

More from Malla Reddy University

Unit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptxUnit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptx
Malla Reddy University
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
Unit 1 - Statistics (Part 1).pptx
Unit 1 - Statistics (Part 1).pptxUnit 1 - Statistics (Part 1).pptx
Unit 1 - Statistics (Part 1).pptx
Malla Reddy University
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
GIS - Project Planning and Implementation
GIS - Project Planning and ImplementationGIS - Project Planning and Implementation
GIS - Project Planning and Implementation
Malla Reddy University
 
Geo-spatial Analysis and Modelling
Geo-spatial Analysis and ModellingGeo-spatial Analysis and Modelling
Geo-spatial Analysis and Modelling
Malla Reddy University
 
GIS - Topology
GIS - Topology GIS - Topology
GIS - Topology
Malla Reddy University
 
Geographical Information System (GIS)
Geographical Information System (GIS)Geographical Information System (GIS)
Geographical Information System (GIS)
Malla Reddy University
 
Introduction to Maps
Introduction to MapsIntroduction to Maps
Introduction to Maps
Malla Reddy University
 
Fluid Mechanics - Fluid Dynamics
Fluid Mechanics - Fluid DynamicsFluid Mechanics - Fluid Dynamics
Fluid Mechanics - Fluid Dynamics
Malla Reddy University
 
Fluid Kinematics
Fluid KinematicsFluid Kinematics
Fluid Kinematics
Malla Reddy University
 
Fluid Mechanics - Hydrostatic Pressure
Fluid Mechanics - Hydrostatic PressureFluid Mechanics - Hydrostatic Pressure
Fluid Mechanics - Hydrostatic Pressure
Malla Reddy University
 
Fluid Mechanics - Fluid Pressure and its measurement
Fluid Mechanics - Fluid Pressure and its measurementFluid Mechanics - Fluid Pressure and its measurement
Fluid Mechanics - Fluid Pressure and its measurement
Malla Reddy University
 
Fluid Mechanics - Fluid Properties
Fluid Mechanics - Fluid PropertiesFluid Mechanics - Fluid Properties
Fluid Mechanics - Fluid Properties
Malla Reddy University
 
Reciprocating Pump
Reciprocating PumpReciprocating Pump
Reciprocating Pump
Malla Reddy University
 
E-waste Management
E-waste ManagementE-waste Management
E-waste Management
Malla Reddy University
 
Biomedical Waste Management
Biomedical Waste ManagementBiomedical Waste Management
Biomedical Waste Management
Malla Reddy University
 
Hazardous Waste Management
Hazardous Waste ManagementHazardous Waste Management
Hazardous Waste Management
Malla Reddy University
 
Digital Elevation Model (DEM)
Digital Elevation Model (DEM)Digital Elevation Model (DEM)
Digital Elevation Model (DEM)
Malla Reddy University
 
Introduction to Solid Waste Management
Introduction to Solid Waste ManagementIntroduction to Solid Waste Management
Introduction to Solid Waste Management
Malla Reddy University
 

More from Malla Reddy University (20)

Unit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptxUnit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptx
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
Unit 1 - Statistics (Part 1).pptx
Unit 1 - Statistics (Part 1).pptxUnit 1 - Statistics (Part 1).pptx
Unit 1 - Statistics (Part 1).pptx
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
GIS - Project Planning and Implementation
GIS - Project Planning and ImplementationGIS - Project Planning and Implementation
GIS - Project Planning and Implementation
 
Geo-spatial Analysis and Modelling
Geo-spatial Analysis and ModellingGeo-spatial Analysis and Modelling
Geo-spatial Analysis and Modelling
 
GIS - Topology
GIS - Topology GIS - Topology
GIS - Topology
 
Geographical Information System (GIS)
Geographical Information System (GIS)Geographical Information System (GIS)
Geographical Information System (GIS)
 
Introduction to Maps
Introduction to MapsIntroduction to Maps
Introduction to Maps
 
Fluid Mechanics - Fluid Dynamics
Fluid Mechanics - Fluid DynamicsFluid Mechanics - Fluid Dynamics
Fluid Mechanics - Fluid Dynamics
 
Fluid Kinematics
Fluid KinematicsFluid Kinematics
Fluid Kinematics
 
Fluid Mechanics - Hydrostatic Pressure
Fluid Mechanics - Hydrostatic PressureFluid Mechanics - Hydrostatic Pressure
Fluid Mechanics - Hydrostatic Pressure
 
Fluid Mechanics - Fluid Pressure and its measurement
Fluid Mechanics - Fluid Pressure and its measurementFluid Mechanics - Fluid Pressure and its measurement
Fluid Mechanics - Fluid Pressure and its measurement
 
Fluid Mechanics - Fluid Properties
Fluid Mechanics - Fluid PropertiesFluid Mechanics - Fluid Properties
Fluid Mechanics - Fluid Properties
 
Reciprocating Pump
Reciprocating PumpReciprocating Pump
Reciprocating Pump
 
E-waste Management
E-waste ManagementE-waste Management
E-waste Management
 
Biomedical Waste Management
Biomedical Waste ManagementBiomedical Waste Management
Biomedical Waste Management
 
Hazardous Waste Management
Hazardous Waste ManagementHazardous Waste Management
Hazardous Waste Management
 
Digital Elevation Model (DEM)
Digital Elevation Model (DEM)Digital Elevation Model (DEM)
Digital Elevation Model (DEM)
 
Introduction to Solid Waste Management
Introduction to Solid Waste ManagementIntroduction to Solid Waste Management
Introduction to Solid Waste Management
 

Recently uploaded

PLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptx
PLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptxPLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptx
PLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptx
Priya Jha
 
C++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHatC++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
A beginner’s guide to project reviews - everything you wanted to know but wer...
A beginner’s guide to project reviews - everything you wanted to know but wer...A beginner’s guide to project reviews - everything you wanted to know but wer...
A beginner’s guide to project reviews - everything you wanted to know but wer...
Association for Project Management
 
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
ALBERTHISOLER1
 
PRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdf
PRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdfPRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdf
PRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdf
nservice241
 
MATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docxMATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docx
yardenmendoza
 
Dreams Realised by mahadev desai 9 1.pptx
Dreams Realised by mahadev desai 9 1.pptxDreams Realised by mahadev desai 9 1.pptx
Dreams Realised by mahadev desai 9 1.pptx
AncyTEnglish
 
Class 6 English Chapter 1 Fables and Folk Stories
Class 6 English Chapter 1 Fables and Folk StoriesClass 6 English Chapter 1 Fables and Folk Stories
Class 6 English Chapter 1 Fables and Folk Stories
sweetygupta8413
 
V2-NLC-Certificate-of-Completion_Learner.docx
V2-NLC-Certificate-of-Completion_Learner.docxV2-NLC-Certificate-of-Completion_Learner.docx
V2-NLC-Certificate-of-Completion_Learner.docx
302491
 
Pedagogy/Definition/Features/Approaches/Types
Pedagogy/Definition/Features/Approaches/TypesPedagogy/Definition/Features/Approaches/Types
Pedagogy/Definition/Features/Approaches/Types
SobiaAlvi
 
DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008
Glenn Rivera
 
Production Technology of Mango in Nepal.pptx
Production Technology of Mango in Nepal.pptxProduction Technology of Mango in Nepal.pptx
Production Technology of Mango in Nepal.pptx
UmeshTimilsina1
 
VRS An Strategic Approch to Meet Need of Organisation.pptx
VRS An Strategic Approch to Meet Need of Organisation.pptxVRS An Strategic Approch to Meet Need of Organisation.pptx
VRS An Strategic Approch to Meet Need of Organisation.pptx
Banker and Adjunct Lecturer
 
Lecture Notes Unit4 Chapter13 users , roles and privileges
Lecture Notes Unit4 Chapter13 users , roles and privilegesLecture Notes Unit4 Chapter13 users , roles and privileges
Lecture Notes Unit4 Chapter13 users , roles and privileges
Murugan146644
 
2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Alvaro Barbosa
 
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
mansk2
 
Java MCQ Questions and Answers PDF By ScholarHat
Java MCQ Questions and Answers PDF By ScholarHatJava MCQ Questions and Answers PDF By ScholarHat
Java MCQ Questions and Answers PDF By ScholarHat
Scholarhat
 
Demonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 SlidesDemonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 Slides
Celine George
 

Recently uploaded (20)

PLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptx
PLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptxPLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptx
PLANT KINGDOM GYMNOSPERM _PRIYA JHA.pptx
 
C++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHatC++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHat
 
A beginner’s guide to project reviews - everything you wanted to know but wer...
A beginner’s guide to project reviews - everything you wanted to know but wer...A beginner’s guide to project reviews - everything you wanted to know but wer...
A beginner’s guide to project reviews - everything you wanted to know but wer...
 
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
 
PRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdf
PRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdfPRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdf
PRESS RELEASE - UNIVERSITY OF GHANA, JULY 16, 2024.pdf
 
MATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docxMATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docx
 
Dreams Realised by mahadev desai 9 1.pptx
Dreams Realised by mahadev desai 9 1.pptxDreams Realised by mahadev desai 9 1.pptx
Dreams Realised by mahadev desai 9 1.pptx
 
Class 6 English Chapter 1 Fables and Folk Stories
Class 6 English Chapter 1 Fables and Folk StoriesClass 6 English Chapter 1 Fables and Folk Stories
Class 6 English Chapter 1 Fables and Folk Stories
 
V2-NLC-Certificate-of-Completion_Learner.docx
V2-NLC-Certificate-of-Completion_Learner.docxV2-NLC-Certificate-of-Completion_Learner.docx
V2-NLC-Certificate-of-Completion_Learner.docx
 
Pedagogy/Definition/Features/Approaches/Types
Pedagogy/Definition/Features/Approaches/TypesPedagogy/Definition/Features/Approaches/Types
Pedagogy/Definition/Features/Approaches/Types
 
DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008
 
UM “ATÉ JÁ” ANIMADO! . .
UM “ATÉ JÁ” ANIMADO!                        .            .UM “ATÉ JÁ” ANIMADO!                        .            .
UM “ATÉ JÁ” ANIMADO! . .
 
Production Technology of Mango in Nepal.pptx
Production Technology of Mango in Nepal.pptxProduction Technology of Mango in Nepal.pptx
Production Technology of Mango in Nepal.pptx
 
VRS An Strategic Approch to Meet Need of Organisation.pptx
VRS An Strategic Approch to Meet Need of Organisation.pptxVRS An Strategic Approch to Meet Need of Organisation.pptx
VRS An Strategic Approch to Meet Need of Organisation.pptx
 
Lecture Notes Unit4 Chapter13 users , roles and privileges
Lecture Notes Unit4 Chapter13 users , roles and privilegesLecture Notes Unit4 Chapter13 users , roles and privileges
Lecture Notes Unit4 Chapter13 users , roles and privileges
 
2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx
 
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
 
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
 
Java MCQ Questions and Answers PDF By ScholarHat
Java MCQ Questions and Answers PDF By ScholarHatJava MCQ Questions and Answers PDF By ScholarHat
Java MCQ Questions and Answers PDF By ScholarHat
 
Demonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 SlidesDemonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 Slides
 

Angular Directives

  • 1. ANGULAR DIRECTIVES (UNIT 3) Dr. P. Rambabu, M. Tech., Ph.D., F.I.E. 30-May-2022
  • 2. Unit 3: Directives Built-in Directives: • How to use Built-in Directives: • Types:  Component Directives  Structural Directives  Attribute Directives. Custom Directives: • Creating a Custom Attribute Directive • Creating a Custom Directive with a Component
  • 6. Built-in Directives Directives extend the behavior of HTML, enabling you to create custom HTML elements, attributes, and classes with functionality specific to an application. Angular provides many built-in directives, which provide the capability to interact with form elements, bind data in a component to the view, and interact with browser events. Using Built-in Directives Much of the Angular functionality that you need to implement in HTML elements is provided through built-in directives. These directives provide a wide variety of support for Angular applications. The following sections describe most of the Angular directives, which fall into three categories: 1. Component: A directive with a template 2. Structural: A directive that manipulates elements in the DOM 3. Attribute: A directive that manipulates the appearance and behavior of a DOM element
  • 7. Components Directives Directives in Angular is a .js class, which is declared as @directive. We have 3 directives in Angular. A @Component decorator is actually a @Directive decorator extended with template- oriented features. Whenever Angular renders a directive, it changes the DOM according to the instructions given by the directive. Directive appears within an element tag similar to attributes.
  • 8. Structural Directives Structural directives alter layout by adding, removing, and replacing elements in DOM. *ngIf When this directive is present in an element, that element is added to the DOM if the value returns true. If the value returns false, then that element is removed from the DOM, preventing that element from using resources. Here is an example: <div *ngIf="students.length > 0"> ….. </div *ngFor This directive is used to create a copy of a template for each item within an iterable object. Here is an example: <div *ngFor="let student of students"> <div>Name: {{student.name}} </div> </div>
  • 9. *ngIf Syntax: <p *ngIf="condition"> content to render, when the condition is true </p> app.component.ts contacts:any = [ {idno:"101",name: "Sravani", email:"sravani@gmail.com"}, {idno:"102",name: "Abhinandan", email:"abhinandan@gmail.com"}, {idno:"103",name: "Maruti", email:"maruti@gmail.com"}, {idno:"104",name: "Kamalesh", email:"kamalesh@gmail.com"} ]; app.component.html <div *ngIf="contacts.length>0"> <h5 class="text-center text-success">Some Content is there to display</h5> </div>
  • 10. *ngIf with else condition: Syntax: <div *ngIf="condition; then normal else elseBlock"></div> <ng-template #normal> Content to render when condition is true. </ng-template> <ng-template #elseBlock> Content to render when condition is false. </ng-template> app.component.ts: contacts:any = []; app.component.ts: <div *ngIf=" contacts.length >0; then fullContent else emptycontent">here is ignored</div> <ng-template #fullContent> <h3 class="text-center text-success">Some content is thre in declared variable</h3> </ng-template> <ng-template #emptycontent> <h3 class="text-center text-danger">provided variable dont have content</h3> </ng-template>
  • 11. *ngFor This directive works like a loop. app.component.ts: contacts:any = [ {idno:"101",name: "Sravani", email:"sravani@gmail.com"}, {idno:"102",name: "Abhinandan", email:"abhinandan@gmail.com"}, {idno:"103",name: "Maruti", email:"maruti@gmail.com"}, {idno:"104",name: "Kamalesh", email:"kamalesh@gmail.com"} ];
  • 12. app.component.html: <table class="table"> <thead> <th>Id No</th> <th>Name</th> <th>Email</th> </thead> <tbody> <tr *ngFor="let contact of contacts"> <td> {{contact.idno}} </td> <td> {{contact.name}} </td> <td> {{contact.email}} </td> </tr> </tbody> </table>
  • 13. ngSwitch This directive displays a template based on the value passed in it. As with ngIf, if the value does not match the case, the element is not created. Here is an example: <div [ngSwitch]=“student"> <span *ngSwitchCase = “ ‘Mohan’ “ > Hello Mohan </span> <span *ngSwitchCase = “ ‘Vijay’ “ > Hello Vijay </span> <span *ngSwitchDefault > Hello Student </span> </div> The ngSwitch directive relies on two other directives to work: ngSwitchCase and ngSwitchDefault. These directives are be explained below. ngSwitchCase: This directive evaluates the value it has stored against the value passed into ngSwitch and determines whether the HTML template it is attached to should be created. ngSwitchDefault: This directive creates the HTML template if all the above ngSwitchCase expressions evaluate to false. This ensures that some HTML is generated no
  • 14. Attribute Directives Angular attribute directives modify how HTML elements look and behave. They are injected straight into the HTML and dynamically modify how the user interacts with an HTML segment. Attribute directives are so named because they often look like normal HTML attributes. ngModel This directive watches a variable for changes and then updates display values based on those changes. Consider these examples: app.component.html: <input type="text" name="data" [(ngModel)]="data" /> <p> {{ data }} </p> app.component.ts:
  • 15. ngStyle This directive updates the styles of an HTML element. app.component.html: Example 1: <div class="col-md-4" [ngStyle]="{'background-color':'green'}">Hello</div> Example 2: <div [ngStyle]="{'background-color’: country === 'UK' ? 'yellow' : 'red' }"> {{country}}</div> app.component.ts: country:any= 'UK’; utput:
  • 16. ngClass It controls the appearance of elements by adding and removing CSS classes dynamically. app.component.html: <div [ngClass]=“ {'text-primary': country === 'UK’}” > {{country}} </div> app.component.ts: country:any= 'UK’; output: UK ngForm This directive creates a form group and allows it to track the values and validation within that form group. By using ngSubmit, you can pass the form data as an object to the submission event. Here is an example: app.component.html: <form #formName="ngForm" (ngSubmit)="onSubmit(formName)"> </form
  • 17. Custom Directives Creating a custom directive is just like creating an Angular component. To create a custom directive we have to replace @Component decorator with @Directive decorator. So, let's get started with creating our first Custom Attribute directive. In this directive, we are going to highlight the selected DOM element by setting an element’s background color. Procedure: Step 1: Create Custom Directive ‘appHighlight’ ng generate directive appHighlight
  • 18. Step 2: Add below code in appHighlight.ts Step 3: Add below code in app.component.html <h1 appAppHighlight > Highlight Me !</h1> import { Directive, ElementRef } from '@angular/core'; constructor(private eleRef: ElementRef) {} ngOnInit() { this.eleRef.nativeElement.style.backgroundColor = "green"; this.eleRef.nativeElement.style.color = "blue"; }
  • 19. Dr. Rambabu Palaka Professor School of Engineering Malla Reddy University, Hyderabad Mobile: +91-9652665840 Email: drrambabu@mallareddyuniversity.ac.in Reference: Node.js, MongoDB and Angular Web Development by Brad Dayley, Brenden Dayley, Caleb Daley