How to create the Custom Directives in Angular

How to create the Custom Directives in Angular

In this post we will give you information about How to create the Custom Directives in Angular. Hear we will give you detail about How to create the Custom Directives in AngularAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to create and use custom directives in angular with the help of examples.

What are Directives?

Directives are custom HTML attributes which tells angular to change the style or behavior of the dom elements.

Creating custom directive

We are creating a custom directive called emoji which helps us to add emojis to our HTML elements.

Open your terminal and run the below command to generate a new directive.

ng generate directive emoji

Now, you can see two new files emoji.directive.ts,emoji.directive.spec.ts that are generated by the angular cli.

Open your emoji.directive.ts file and replace with the below code.

emoji.directive.ts
import { Directive, ElementRef, OnInit } from '@angular/core';@Directive({  selector: '[appEmoji]'})export class EmojiDirective implements OnInit {  constructor(private el: ElementRef) {}   ngOnInit() {    this.el.nativeElement.textContent +=️ '✌️';  }}

In the above code, first we imported Directive decorator and ElementRef type from the @angular/core package.

The @Directive method accepts object as its first argument, where we need to define the name of our directive using selector: ‘[appEmoji]’

Inside the ngOnInit() lifecycle hook method we are accessing the element and adding the victory hand emoji to that element textContent.

Using custom directive

Let’s use our custom directive appEmoji inside the app component.

app.component.html
&lt;div style="<span>text-align:center"&gt;  &lt;h1 appEmoji&gt;Welcome to Angular&lt;/h1&gt;&lt;/div&gt;

Output

Passing values to a custom directive

We can also pass our own emojis to the appEmoji directive instead of using the same emoji, for that we need to import the @Input decorator.

emoji.directive.ts
import { Directive, ElementRef, Input, OnInit } from '@angular/core';@Directive({  selector: '[appEmoji]'})export class EmojiDirective implements OnInit {  @Input('appEmoji') emoji: string;  constructor(private el: ElementRef) { }  ngOnInit() {    this.el.nativeElement.textContent += this.emoji;  }}

Usage

app.component.html
&lt;div style="<span>text-align:center"&gt;  &lt;h1 appEmoji="&#x1f44c;"&gt;Welcome to Angular&lt;/h1&gt;&lt;/div&gt;

Hope this code and post will helped you for implement How to create the Custom Directives in Angular. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US