Angular directive is one of core building blocks of angular which provides additional behavior to elements in your application.
We can attach extra attributes to the markup language parts (elements) which may helps you manipulate the looks of web page.
You know , the components are the building blocks of an angular applications because of directives, components are nothing more than a directive with a templet.
Components have their own templets whereas angular directive has directions to vary visual illustration and rendering the template. Angular 12 is released, grasp what is new in it
Type of directives in angular
- Components directive – directive with template
- Attribute directive – change the appearance and behavior of DOM element
- Structural directive – change layout by adding or removing DOM element
Component directive
errorMessage.directive.ts
import {Directive, ElementRef} from '@angular/core';
@Directive({
selector:'[error-message]'
})
export class ErrorMessageDirective{
constructor(elr:ElementRef){
elr.nativeElement.style.background='red';
}
}
app.component.html
<p error-message>
Hi , my color was changed by angular directive
</p>
Do not forget to declare error-message directive in app.module.ts
Like the above example, we made a custom directive to apply red background for errors in angular application and then we can simply put the directive in DOM elements which element to want with red background.
Attribute Directive
- ngStyle directive
- ngClass directive
NgStyle Directive
<div [ngStyle]="{'border':isConditionMatch ? '4px solid green' : '5px solid red' }">
ngStyle Example
<div>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 6';
isConditionMatch = true;
}
Result:
In the above example we set a border on text “ngstyle example” conditionally using ngStyle directive.
NgClass Directive
.custom-class {
border: 3px solid green
}
app.component.ts
<div [ngClass]="isConditionMatch ? 'custom-class' : '' ">ngStyle Example<div>
Structural Directive
- ngIf
- ngFor
- ngSwitch
- ngIf-else
1. ngIf
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template:
`<h1 *ngIf="1 < 2" >tag 1</h1>
<h1 *ngIf="false" >tag 2</h1>
<h1 *ngIf="property" >property</h1>
<h1 *ngIf="nullValue" >null</h1>`,
})
export class AppComponent {
property = true;
nullValue = null;
}
Output:
![]() |
ngIf directive |
2. NgFor
app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'ngfor-grouped-example',
templateUrl: './grouped-example.component.html',
styleUrls: ['./grouped-example.component.css']
})
export class GroupedExampleComponent {
peopleByCountry: any[] = [
{
'country': 'UK',
'people': [
{
"name": "Douglas Pace"
},
{
"name": "Mcleod Mueller"
},
]
},
{
'country': 'US',
'people': [
{
"name": "Day Meyers"
},
{
"name": "Aguirre Ellis"
},
{
"name": "Cook Tyson"
}
]
}
];
}
app.component.html
<ul *ngFor="let group of peopleByCountry">
<li>{{ group.country }}</li>
<ul>
<li *ngFor="let person of group.people">
{{ person.name }}
</li>
</ul>
</ul>
Output
![]() |
ngFor directive |
3. ngSwitch
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
positions:Array<Object> = [
{
id: 101,
name: 'CEO'
},
{
id: 102,
name: 'CFO'
},
{
id: 103,
name: 'CTO'
},
{
id: 104,
name: 'CMO'
}
]
}
Outputfour
4. ngIf-else
<input [(ngModel)]="showContent" type="checkbox"/> Show My Secret Message
<hr />
<div *ngIf="showContent; else message">
Hello Angular 12!
</div>
<ng-template #message>
Click the checkbox above to read the secret message!
</ng-template>
Ouput:
Angular 12 just released, know what’s new in it