Learn About Some Directives In Angular

Introductions

 
In this article, we are going to see Angular directives. Directives in Angular are a Typescript class which is declared with decorator @Directive. These are DOM instruction sets, which decide how logic implementation can be done. 
The angular directives are classified into three types
  • Attribute directives
  • Structural directives
  • Components directives

Attribute directives

 
The first one, the attribute directives, are used to change the styles and behavior of the DOM element. We use attribute directives to apply a conditional style to elements, show or hide elements or dynamically change the behavior of a component according to a changing property. It provides the ability  to create our custom attribute directive. Please follow the below codes to create  our custom attribute directive.
 
Here I have mentioned existing directives in Angular are fairly easy. The ngClass directive is a good example of an existing Angular attribute directive.
  1. <style>  
  2.    .red{color: red}  
  3.    .green{color: green}  
  4. </style>  
  5. <p[ngClass]="{'red'=true, 'green'=false}">  
  6.    Angular attribute directive  
  7. </p>  
Next, we are going to create a custom attribute directive, Here I have the app.mydirective.ts file. Please copy the below codes.
  1. import {Directive, ElementRef} from'@angular/core';  
  2.    @Directive({  
  3.       selector:'[my-directive]'  
  4.    })  
  5.    exportclassMyDirective{  
  6.    constructor(elr:ElementRef){  
  7.       elr.nativeElement.style.background='yellow';  
  8.     }  
  9. }  
After creating the directive from @angular/core we can then use it. First, we need a selector like my-directive.
 
Then we created a class, MyDirective, to access any element of our DOM, we need to use ElementRef. Since it also belongs to the @angular/core package.it simple to import and use in our application.
 
use this newly created directive to the app.module.ts file.
  1. import { NgModule } from'@angular/core';  
  2. import { BrowserModule } from'@angular/platform-browser';  
  3. import { MyDirective } from'./app.mydirective';  
  4. import { AppComponent } from'./app.component';  
  5. @NgModule({  
  6.    imports: [ BrowserModule ],  
  7.    declarations: [ AppComponent, MyDirective ],  
  8.    bootstrap: [ AppComponent ]  
  9. })  
  10. exportclassAppModule { }  
Next, add the app.component.ts and app.component.html file codes.
  1. import { Component, Input } from'@angular/core';  
  2. @Component({  
  3.    selector:'app-root',  
  4.    templateUrl:'./app.component.html'  
  5. })  
  6. exportclassAppComponent {  
  7.    name = 'Angular Directive ';  
  8. }  
  9. <h3my-directive>Test Sat {{name}}</h3>  
Next After adding the code please run the application using this commends npm start or ng-serve --open, the result will be like this.
 
 

Structural directives

 
The structure directives manipulate the DOM elements. Angular applications already have structure directives. For example, *ngIf and *ngFor. We will see how to use these directives in applications.
  1. import { Component, Input } from'@angular/core';  
  2. @Component({  
  3.    selector:'app-root',  
  4.    templateUrl:'./app.component.html',  
  5. })  
  6. exportclassAppComponent {  
  7.    name = 'Angular Directive ';  
  8.    numberlists=['1''2''3','4''5''6''7'];  
  9. }  
  10. <div*ngFor="let num of numberlists">  
  11.    <ng-container*ngIf = "(num == '1' || num == '3' || num == '5' || num == '7'); else elseTemplate">  
  12.       <h1>{{num}} is a odd number</h1>  
  13.    </ng-container>  
  14.    <ng-template#elseTemplate>  
  15.       <h1>{{num}} is even number</h1>  
  16.    </ng-template>  
  17. </div>  
In the above code, we are used ngFor and ngIf directives and finally, the output will show like the below screens.
 
 

Components directives

 
The components are just directives with templates. Under they use the directive API and give us a cleaner way to define the applications. It contains the details on component processing, instantiation, and usage at run time. The other two directive types don’t have templates. Instead, they are specifically for DOM manipulation.
  1. @Component({  
  2.    selector:'app-root',  
  3.    templateUrl:'./app.component.html',  
  4.    styleUrls: ['./app.component.scss']  
  5. })  
selector - Here I mentioned the template tag which specifies the start and end of the component.
 
templateURL - Template used for the component.
 
styleUrls - It is all the style format files used for the template.
 
I hope this article is most helpful for you.