Learn About Directives In Angular

Introduction

 
In this article, we will see how the different types of directives work in Angular.
 
Angular has three types of directives.
  1. Component
  2. Structural
  3. Directives
Component directives have their own template. It is identified by @Component decorator that defines the class defined is component class. We can define a component view with its template. Template tells Angular how to render the component.
 
Structural directives are used to change the structure of the HTML element. An * is added in the directive attribute name. It is helpful to shape/reshape the DOM’s structure by manipulating the elements. We can use one structural directive for a particular host element.
 
Attribute Directives are used to change the behavior of the DOM element. It uses @Directive annotation decorated on the class.
 
Let’s have an example with the use of component, structure, and attribute directive used in Angular.
  • Create an Angular project.
  • Create a test component.

    Ng g c testDirective

    Directives In Angular
  • Create a directive.

    Ng g c mydir

    Directives In Angular
Open my-dir.directrive.ts and add/modify with the below conent.
  1. import { Directive, ElementRef } from '@angular/core';  
  2.   
  3. @Directive({  
  4.   selector: '[appMyDir]'  
  5. })  
  6. export class MyDirDirective {  
  7.   constructor(el: ElementRef) {   
  8.     el.nativeElement.style.background='red';  
  9.     el.nativeElement.style.width='200px';  
  10.   }  
  11. }  
Open test-directive.component.ts and modify with the below content.
  1. import { Component, OnInit, Input } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test-directive',  
  5.   templateUrl: './test-directive.component.html',  
  6.   styleUrls: ['./test-directive.component.css']  
  7. })  
  8. export class TestDirectiveComponent implements OnInit {  
  9.   @Input('name') name : string;  
  10.   @Input('phone') phone : string;  
  11.   constructor() {}  
  12.   
  13.   ngOnInit() {  
  14.   }  
  15. }  
Open test-directive.component.html and add the below content.
  1. {{name}} : {{phone}}  
  2. <p appMyDir>This text is under custom directive decorated by @directive</p>  
Open app.component.ts and modify with the below code.
  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.   title = 'DirectivesDemo';  
  10.   isVisible : boolean;  
  11.   personStyle : any;  
  12.   
  13.   constructor(){  
  14.     this.isVisible = true;  
  15.     this.personStyle = {   
  16.       'color' : 'white',  
  17.       'background-color' : 'grey',  
  18.       'width' : '200px'  
  19.     }  
  20.   }  
  21. }  
Open app.component.html and add the below code.
  1. <app-test-directive  
  2.   [name] = '"Mohammad Irshad"'  
  3.   [phone] = '"7860000000"'  
  4.   *ngIf="isVisible"  
  5.   [ngStyle]="personStyle"  
  6. ></app-test-directive>  
Angular CLI will automatically add the components in declarations.
 
Open app.module.ts
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3.   
  4. import { AppRoutingModule } from './app-routing.module';  
  5. import { AppComponent } from './app.component';  
  6. import { PersonComponent } from './person/person.component';  
  7. import { TestDirectiveComponent } from './test-directive/test-directive.component';  
  8. import { MyDirDirective } from './my-dir.directive';  
  9.   
  10. @NgModule({  
  11.   declarations: [  
  12.     AppComponent,  
  13.     TestDirectiveComponent,  
  14.     MyDirDirective  
  15.   ],  
  16.   imports: [  
  17.     BrowserModule,  
  18.     AppRoutingModule  
  19.   ],  
  20.   providers: [],  
  21.   bootstrap: [AppComponent]  
  22. })  
  23. export class AppModule { }  
We have used attribute directives that we have created as custom attribute directive ‘appMyDir’ under TestDirectiveComponent.
 
TestDirectiveComponent has been used under appComponent.
 
We have used the structural directive *ngIF that is provided by Angular as it is a structural directive and [NgStyle] attribute directive that is also provided by Angular.
 
Run the application. You will see the output defined by directives element.
 
Directives In Angular
So, we are done with the explanation of different types of directives.


Similar Articles