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.


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

TS file:

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


  selector: 'app-animation',

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

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


export class AnimationComponent implements OnInit {

  constructor() { }

public animateDiv(receivedObj){




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 } 

export class ItemsComponent {

  public items = [





  public getBgColor(type){

    if(type == 'fruit'){

      return 'blue';


      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.


<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.



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

TS File:

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


  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';





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.

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}