{{announcement.body}}
{{announcement.title}}

Angular Directives Tutorial

DZone 's Guide to

Angular Directives Tutorial

In this post, we go over three popular directive in Angular and show you how to implement them in your code.

· Web Dev Zone ·
Free Resource

A directive is an add-on or an extension to Angular features, used for optimizing your reusable code.

There are three types of directives in Angular (component, attribute, and structural).

Component Directives

A component is a directive with a template. Basically, whatever we do in Angular is a component. It is a reusable part of the whole application.

Here is an example of a simple animation component that rotates the div by 180 degrees when clicked.

Template:

<div (click)="animateDiv(animatediv)" #animatediv class="animateDiv">CLICK ME</div>

TS file:

import { Component, OnInit } from '@angular/core';

@Component({

  selector: 'app-animation',

  templateUrl: './animation.component.html',

  styleUrls: ['./animation.component.css']

})

export class AnimationComponent implements OnInit {

  constructor() { }

public animateDiv(receivedObj){

 receivedObj.style.;

  }

}

Attribute Directives

An attribute directive changes the way a component, element, or directive looks. Attribute directives are used to give a better look and feel to our element. Some of the commonly used attribute directives in Angular are: ngStylengSwitchngModelngClass.

Below is an example of a simple component showing how to use the ngStyle directive. It assigns the background color as blue if the item is a fruit and green if it is a vegetable.

<ul *ngFor="let item of items">
  <li class="item" [ngStyle]="{'backgroundColor':getBgColor(item.type)}" > 
    {{ item.name } 
  </li>
</ul>


export class ItemsComponent {

  public items = [

    {id:1,name:'APPLE',type:'fruit'},

    {id:1,name:'TURNIP',type:'vegetable'},

    {id:1,name:'ORANGE',type:'fruit'}

  ];

  public getBgColor(type){

    if(type == 'fruit'){

      return 'blue';

    }else{

      return 'green';

    }

  }

}

Structural Directives

As the name suggests, a structural directive changes the structure of a component, element, or directive. Basically, it is used to manipulate the DOM. Some of the commonly used structural directives in Angular are ngIf and ngFor

Below is a simple component showing the usage of the ngIf directive. This component toggles the div on the click of the button.

Template:

<button (click)='toggleDiv()'>TOGGLE</button>

<div class="showDiv" *ngIf=showDiv>I AM VISIBLE</div>

TS file:

export class ToggleComponent {

public showDiv:boolean = true; 

public toggleDiv(){

  this.showDiv = !this.showDiv;

}

}

Building Custom Directives

We can also create custom directives based on our requirements and use them throughout the application.

Example

Template:

<p appPaintGreen>I AM A GREEN PARAGRAPH</p>

TS File:

import { Directive, ElementRef } from '@angular/core';

@Directive({

  selector: '[appPaintGreen]'

})

export class PaintGreenDirective {

  constructor(private el:ElementRef) {

    // el here contains the object in which you are using this directive

    el.nativeElement.style.backgroundColor = 'green';

    el.nativeElement.;

  }

}

Conclusion

Directives are basically custom elements that give some extra capability to an element. They can be used to make our components flexible in both appearance and functionality.

Topics:
angular directive ,web dev ,angular tutorial ,typescript tutorials ,angular components

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}