Angular Directives Tutorial

Angular Directives Tutorial

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

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

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 irectived. 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)>TOGGLE</button>

<div class="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 based to make our components flexible in both appearance and functionality.

Originally published by Mamatamayee parida at https://dzone.com

Learn More

☞ Angular 7 (formerly Angular 2) - The Complete Guide

☞ Learn and Understand AngularJS

☞ Angular Crash Course for Busy Developers

☞ The Complete Angular Course: Beginner to Advanced

☞ Angular (Angular 2+) & NodeJS - The MEAN Stack Guide

☞ Become a JavaScript developer - Learn (React, Node,Angular)

☞ Angular (Full App) with Angular Material, Angularfire & NgRx

☞ The Web Developer Bootcamp

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Use Cookies in Angular for Storing user’s Credentials

In this post, I will be explaining about using Cookies in Angular for Storing user’s Credentials

Angular Tutorial - Learn Angular from Scratch

Angular Tutorial - Learn Angular from Scratch: This course is for beginners who are curious on how to get started with Angular. In this course you will learn how to download, install and play around with Angular. We teach you the main components of Angular, so that you can get up and running with it asap. You will learn now to start building applications with Angular.

An Angular Roadmap — The Past, Present, and Future of Angular

✅Interested in being an Angular developer in 2019? ... blog post it's most likely that you've written some code in javaScript in the past.