Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Customdirective in angular

How to Create Custom Directive in Angular 12?

Aman Jain, October 12, 2021February 8, 2024

In this tutorial we will learn to create the custom directive in angular and how to use them in our project. Custom directives are those directives by which we can enhance the html capabilities and can bind custom attributes, methods to it. We can reuse them in any element or custom html tag. In angular directives are almost same as component but in directives we can perform some additional tasks as well like hovering, host elements, binding etc. let’s begin with Custom Directives Tutorial in Angular

Types of Custom directive in angular

In angular there is two types of directives

  1. Attributes Directives
  2. Structural Directives
  1. Attributes Directives : Attributes directives are used to change behavior or appearance of the html examples are NgClass, NgStyle.
  2. Structural Directives: Structural Directives are used to add or remove the elements from the Dom examples are *ngIf, *ngFor etc. Structural directives start with *.

In this tutorial we will cover the attribute directive so Let’s understand it by step by step process.

Step 1 : Create a new custom diretive

You can simply create a custom directive using angular cli

ng generate directive myInit

this will create two files src/app/my-init.directive.ts and src/app/my-init.directive.spec.ts. It will also import the new directive to our module app.module.ts.

You can also create the file manually and add it to the modules and add simple code

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

@Directive({
  selector: '[appMyInit]'
})
export class MyInitDirective {

  constructor() { }

}

Here, we have added @Directive and added selector to appMyInit which is name of our directive.

Step 2: Import the new directive to module

If you have created the file manually then its required to add it into our module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { MyInitDirective } from './my-init.directive';

@NgModule({
  declarations: [
    AppComponent,
    MyInitDirective
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 3 : Add logic into MyInitDirective

import { Directive,ElementRef } from '@angular/core';
@Directive({
  selector: '[appMyInit]'
})
export class MyInitDirective {

  constructor(el: ElementRef){
    el.nativeElement.style.backgroundColor = 'yellow';
  }
  ngOnInit() {
      
  }



}

Here, we have injected ElementRef to find the element and change the appereance.

Step 4: Using new directive into html element

Now, we are going to use our newly created directive to any element.

<h1>Angular 2 custom ngInit directive  -ReaderStacks.com </h1>

 Original Title : {{title}}

<div appMyInit>
   Updated title in html view : {{title}}

</div>
 

and component

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'rest-api';
}

Changing Appearance on user events

We can also handle appearance on user events like click, hover, mouse enter, leave etc. by adding the HostListener in angular.

@HostListener('click', ['$event.target']) 
onClick() {
    this.el.nativeElement.style.backgroundColor ='red';
}
 

Check the example and download code:

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

Javascript get radio input value in jQuery

How to get radio input value in jQuery ?

October 31, 2021November 5, 2023

While submitting a form or validating a form using the jQuery, we can get all the values of form easily but radio button works differently and we need some extra efforts to fetch the value of radio input. So in this article i will demonstrate to fetch the value of…

Read More

How to submit multipart form data in angular reactive form ?

September 12, 2021October 31, 2021

In this tutorial, you will learn uploading a multipart reactive form in angular using HttpClient. In a webpage uploading files as multipart form data is a important aspect. If you are a new then you can read What is Angular and how to install angular ? So, for this tutorial…

Read More
Uncategorized update page data without reload

How to update page data without reload in jQuery?

October 24, 2021November 17, 2023

If you are beginner then this post is for you. For showing the new content on page without refresh we will use JQuery Reload Page without Refresh and server side PHP. In this article, I will demonstrate to show the latest content from server without refreshing the page. This is…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version