Custom Directives in Angular

Posted by: admin
Category: Angular

Directives in Angular

Angular directives are the functions that are executed whenever Angular compiler identifies it. As the name suggests Angular directives increase the capability of HTML elements by attaching custom behaviors to the DOM i.e. Giving direction to the HTML elements.

Types of directives

There are three types of directives in Angular.

  1. Attribute Directives

The Directives are used for changing appearance and behavior of DOM elements that is responsible to render HTML elements and attaching a behavior. We can use it to change the style of DOM elements by making runtime changes in the CSS Styles and its classes.

These directives are also used to conditionally hide or show particular DOM elements. There are many built-in Attribute Directives like NgStyleNgClass, etc.

  1. Structural Directives

The Directives are used for changing the structure of the DOM. It is generally used for dynamically adding or removing the elements from the DOM.

You can easily differentiate between the Structural and Attribute Directive from it’s syntax. The Structural Directive’s name always starts with an asterisk (*) prefix and Attribute Directive does not contain any prefix.

There are three most popular Structural Directives.

*NgIf.

*NgFor.

*NgSwitch.

  1. Components Directives

Components are directives with templates. Template is the only difference between Components and the other two types of directives. So, we can say that the Component is a cleaner version of the Directive with a template, These helps us to reuse the code snippet as and when required, making sure that the concept of OOPS are being implemented in the angular framework.

Exploring Custom Directives

Custom Directives are used in Angular to extend the functionality of HTML. These are defined by ‘directive’ function. A custom directive just replaces the element for which it is activated.

Custom Attribute Directive

For creating a custom directive, we need to creating an Angular component and also we have to replace @component decorator with the @directive decorator.

So, here we are going to create our first Custom Attribute directive and In this directive we are highlighting the selected DOM element by displaying an element’s background color.

In this application first of we are creating a directive file call as ‘highlight.directive.ts’ in src/app folder and add below mention code into it.

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

@Directive({

  selector: '[appHighlight]'

})

export class HighlightDirective {

constructor(elementRef: ElementRef) {

elementRef.nativeElement.style.backgroundColor = 'red'

  }

}

So, here we have imported Directive and ElementRef from angular-core framework. Directive providing the functionality of @directive decorator where we provide its property selector as a ‘appHighLight’ So that we can use it across the application. We have also implemented ‘ElementRef’ which is work for accessing the DOM element.

Now we need to add our Directive in the ‘app.module.ts’ file So we can use it across the application.

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';

import { HighlightDirective } from './highlight.directive';

@NgModule({

  declarations: [

    AppComponent,

    HighlightDirective

  ],

imports: [

    BrowserModule,

    AppRoutingModule

  ],

providers: [],

  bootstrap: [AppComponent]

})

export class AppModule { }

Now we can use our custom directive. I am adding ‘appHighlight’ directive in the ‘app.component.html’ file but you can use it as per your requirement in the application.

<h1 appHighlight>Hello Arpit</h1>

The output are as below.

Custom Structural Directive

As we have created Attribute directive before same as we are creating Structural directive as well.

In this directive we are going to implement the *appNot directive which is work opposite to *ngIf.

So, first of we need to create a directive file call as ‘appNot.directive.ts’ in src/app folder and add below mention code into it.

import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core';

@Directive({

  selector: '[appNot]'

})

export class AppNotDirective {

  constructor(

    private templateRef: TemplateRef<any>,

    private viewContainer: ViewContainerRef) { }

    @Input() set appNot(condition: boolean) {

        if (!condition) {

            this.viewContainer.createEmbeddedView(this.templateRef);

        } else {

            this.viewContainer.clear();}

    }

}

Here we have implemented Directive, Input, TemplateRef and ViewContainerRef from @angular/core.

The Input decorator is a communication bridge between the two components. It is sending data from one component to other component using property binding.

TemplateRef used for the embedded template and it is used to define embedded views and these views are linked with the template.

Now we need to add our Directive in the ‘app.module.ts’ file as we added for Attribute directive.

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';

import { HighlightDirective } from './highlight.directive';

import { AppNotDirective } from './app-not.directive';

 

@NgModule({

  declarations: [

    AppComponent,

    HighlightDirective,

    AppNotDirective

  ],

  imports: [

    BrowserModule,

    AppRoutingModule

  ],

  providers: [],

  bootstrap: [AppComponent]

})

export class AppModule { }

Now we can use our custom directive. I am adding ‘appHighlight’ directive in the ‘app.component.html’ file but you can use it as per your requirement in the application.

<h1 *appNot=”true”>True</h1><h1 *appNot=”false”>False</h1>

The output are as below.

Structural Directive Output
Structural Directive Output

 

 

 

 

Custom Directives allows the developer to way to enhance the various components and elements of the HTML clan as per requirements. It also allows the developer to reuse the same functionality across the application if required with minimal efforts while constructing them as well as while maintain them in the long run.

Author : Arpit Prakashbhai Patel

Let’s build your dream together.

Aceh4D Aceh4D slot gacor slot gacor slot gacor Aceh4D slot gacor aceh4d slot gacor slot gacor slot gacor aceh4d aceh4d aceh4d toto slot toto slot toto slot toto slot toto slot toto slot toto slot toto slot toto slot toto slot aceh4d aceh4d aceh4d slot online slot toto slot bet 400 acehbola Aceh4d Slot Big Win scatter hitam Situs Slot, togel online tus4d scatter hitam Situs Bet 100 perak acehbola acehbola, slot online acehbola, Toto Togel Slot Gacor Slot Gacor Slot Gacor Parlay Bola Acehbola aceh4d aceh4d aceh4d, situs toto 4d aceh4d, situs togel Slot Gacor acehbola Tus4D acehbola tus4d, slot online, bandar togel aceh4d, slot online, slot bet 100 aceh4d, slot toto acehbola, Link Togel acehbola, slot bet 200 aceh4d acehbola, slot toto aceh4d aceh4d aceh4d scatter hitam tus4d, situs toto & togel 4d aceh4d acehbola acehbola, toto togel acehbola aceh4d aceh4d tus4d acehbola Agen Toto 4D slot bet 200 acehbola, slot toto Toto Slot slot dana slot qris dana Slot Online Gacor Slot Online Aceh4D Scatter Hitam slot bet 200 aceh4d Slot gacor Slot gacor Slot gacor Slot gacor Slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot toto togel slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot toto slot gacor slot gacor slot gacor slot gacor slot gacor SLOT ONLINE acehbola slot toto slot gacor slot gacor Acehbola, Agen Togel Agen Slot Gacor Slot Gacor Slot Toto Toto Bet 200 slot gacor ACEH4D slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor slot gacor Slot Online toto slot Slot bet 200 Scatter Hitam poker online slot gacor toto slot situs togel SLOT GACOR SLOT GACOR aceh4d jmkbet jmkbet SLOT GACOR Bandar Toto Togel Toto Macau 4D Toto Macau Aceh4d Aceh4d Aceh4d Aceh4d Aceh4d Aceh4d Aceh4d Aceh4d-slot-toto aceh4d-link-alternatif acehbola jmkbet Daftar Aceh4d tus4d login neototo situs aceh4d tus4d-login aceh4d Aceh4d Aceh4d gacor Aceh4d Tus4D slot toto slot toto Lapak7d aceh4d aceh4d acehbola Neototo Neototo Neototo Neototo Neototo Neototo Neototo lapak7d neototo neototo neototo jmkbet aceh4d jmkbet aceh4d aceh4d aceh4d Toto Slot tus4d Judi Slot Gacor neototo Aceh4d Acehbola toto slot 4d neototo jmkbet Aceh4D togel online neototo aceh4d Aceh4D acehbola acehbola Slot Toto Gacor slot gacor slot gacor Aceh4D acehbola acehbola acehbola Aceh4D Aceh4D acehbola bacan4d bacan4d bacan4d Slot Toto Slot Toto aceh4d, slot qris Aceh4D Aceh4D Aceh4D AcehBola Slot Gacor Aceh4D Aceh4D

aceh4d

aceh4d

neototo

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

Slot Gacor

ACEH4D

ACEH4D

login aceh4d

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

re

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

slot gacor

slot gacor

slot gacor

slot gacor hari ini

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

slot gacor hari ini

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

bobatoto

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

slot gacor

slot gacor hari ini

slot gacor hari ini

slot maxwin

slot gacor

aceh4d

toto slot

aceh4d

slot gacor

slot qris

slot qris

toto slot

toto slot

aceh4d

toto slot

toto slot

toto slot

toto slot

arahtogel

jpmania

toto slot

toto slot

toto slot

toto slot

toto slot

toto slot

Slot gacor

aceh4d

slot gacor hari ini

toto slot

toto slot

Slot gacor

Slot gacor

Slot gacor

Poker Online

slot gacor

Poker Online

slot gacor hari ini

Poker Online

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

slot gacor

Bandar togel toto

Slot dana

Toto slot gacor

Bandar Toto 4D

Agen toto 88

Toto togel 4d

Bandar togel resmi

slot gacor

Scatter hitam

slot toto togel

Slot dana

Bandar togel online

Situs slot

slot toto togel

slot gacor

slot gacor

Bandar togel 4D

aceh4d login

toto slot

slot gacor

toto slot

slot gacor

aceh4d

aceh4d

aceh4d

aceh4d

Slot qris

slot qris

Bandar togel online

Togel online

aceh4d login

Slot gacor

Aceh4d

Togel Online

toto slot

slot toto

slot gacor

Togel toto macau

ACEH4D

ACEH4D

SLOT GACOR

SLOT GACOR

SLOT GACOR

Slot toto gacor

Togel online

Slot toto

Slot Toto

Slot qris

Slot gacor

aceh4d

Slot gacor

tus4d

tus4d

Slot gacor

Aceh4d

slot gacor

slot gacor

slot toto

ACEH4D

Slot qris

slot online

scatter hitam

slot gacor

slot gacor

slot indonesia

togel online

aceh4d

slot gacor

tus4d

Togel toto online

Slot Gacor

Slot Toto

Bandar togel online

Slot indonesia

toto slot gacor

scatter hitam

slot gacor

slot toto gacor

aceh4d

neototo

slot indonesia

acehbola

situs toto slot

ACEH4D

acehbola

slot toto gacor

slot gacor

toto slot

slot gacor

aceh4d

toto slot

toto slot

Slot online gacor

aceh4d

aceh4d

Slot online

https://stitypilahat.ac.id/

ACEH4D LOGIN

Slot toto gacor

Bandar togel online

acehbola

ACEH4D LOGIN

acehbola

aceh4d

Toto slot

Scatter hitam

jmkbet

aceh4d

ACEH4D

ACEH4D

ACEH4D

ACEH4D

ACEH4D

slot gacor

slot toto gacor

slot gacor

slot toto

togel toto macau

slot resmi gacor

slot gacor

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

Slot toto

acehbola

acehbola

slot gacor

ACEH4D

ACEH4D

aceh4d

ACEH4D

tus4d

tus4d

Slot gacor

ACEH4D

ACEH4D

SCATTER HITAM

aceh4d

aceh4d

aceh4d

slot toto

slot toto

slot toto

slot toto

aceh4d

slot toto

slot gacor

slot toto

aceh4d

aceh4d

scatter hitam

scatter hitam

scatter hitam

Toto slot

ACEH4D

scatter hitam

scatter hitam

scatter hitam

scatter hitam

ACEH4D

ACEH4D

slot toto

tus4d

situs toto

slot toto

aceh4d

slot maxwin

aceh4d

slot toto

tus4d

tus4d

acehbola

cie4d

slot toto gacor

slot online

ACEH4D

slot gacor online

slot toto 4d

toto 4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

scatter hitam

toto slot gacor

slot toto

slot toto

situs togel

situs togel

tus4d

tus4d

aceh4d

toto slot

bobatoto

situs judi online terpercaya

slot online gacor hari ini

togel online terpercaya

bandar togel resmi

link slot gacor terbaru

situs slot dan togel terpercaya

acehbola

acehbola

acehbola

acehbola

toto slot

slot zeus

slot toto

togel toto 4d

toto slot gacor

toto 4d

slot gacor

slot toto

slot dana

depo 5000

situs togel

slot maxwn

toto togel

slot gacor

aceh4d

toto macau

slot toto

situs togel

togel online

togel toto 4d

slot gacor

slot dana

slot gacor

neototo

neototo

toto gacor

neototo

slot gacor

slot gacor

slot toto

slot qris

scatter hitam

slot qris

slot dana

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

toto slot gacor

aceh4d

slot gacor

toto slot

aceh4d

aceh4d

aceh4d

slot toto

slot toto

aceh4d

slot maxwin

situs toto

slot gacor

toto slot gacor

toto slot

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

slot gacor

slot gacor

slot gacor

toto gacor

acehbola

acehbola

acehbola

acehbola

acehbola

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

aceh4d

toto slot

aceh4d

aceh4d

aceh4d

slot dana

situs toto

slot maxwin

slot gacor

slot thailand

situs toto

slot dana

rtp slot

aceh4d slot

ACEHBOLA

aceh4d

situs gacor

ACEHBOLA

jmkbet

neototo

aceh4d

aceh4d

neototo

ACEHBOLA

neototo

aceh4d

aceh4d

aceh4d

aceh4d

slot dana

situs toto

situs toto

situs toto

slot toto

acehbola

situs toto

slot gacor

ACEHBOLA

ACEHBOLA

ACEH4D

SLOT DANA

ACEHBOLA

SITUS TOTO GACOR

SITUS SLOT TOTO

neototo

neototo

aceh4d

acehbola

aceh4d

aceh4d

slot gacor

acehbola

toto slot

acehbola

acehbola

Neototo

Slot Toto

toto slot

aceh4d

slot 4d

slot toto

aceh4d

situs judi

slot gacor

toto slot

scatter hitam

toto slot

slot thailand

aceh4d

aceh4d

slot toto

slot toto

aceh4d

aceh4d

slot gacor

slot dana

Slot Gacor

Situs togel

Slot Gacor

https://ppid.isi-padangpanjang.ac.id/

scatter hitam

togel online

slot toto

togel online

slot gacor

toto slot

aceh4d

slot gacor

neototo

slot 4d

toto gacor

aceh4d

aceh4d

aceh4d

slot maxwin

slot gacor

slot gacor

situs slot

slot dana

slot gacor

slot online

slot toto

slot maxwin

aceh4d

slot dana

slot toto

neototo

jmkbet

tus4d

aceh4d

slot toto

Bokep Indonesia

Bokep Sma