How To Make Custom Directive In Angular2
Posted By : Rohit Goyal | 30-Jun-2017
Introduction:-
Custom directive are always useful for adding special functionality on an element.We have make it once and we can use it anywhere
by importing this directive. purpose of diretives to increase reusability.Let's begin:-
1) Make a component:-
here we specify highlight directive and bcolor attribute.so that we can background color in directive.
import { Component } from '@angular/core'; @Component({ selector:'child1', template:` <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 align="center">Component 1</h3> </div> </div> <div class="panel-body"> <button highlight bcolor="orange" class="btn btn-sm">Click</button> <h3 highlight bcolor="red">Hello1 Directive</h3> <p highlight bcolor="green">Hello2 Directive</p> </div> </div> ` }) export class childComponent1{ }
2) Make a custom directive typescript file
Directives are not singleton.They will create sepearte instannce whenever the directive is being called
(i) ElementRef used to find out directive where it is applied
(ii) Input set input value from outside
import {Directive} from '@angular/core'; import {ElementRef} from '@angular/core'; import {Input} from '@angular/core'; @Directive({ selector:'[highlight]' // for directive will be defined inside square brackets }) export class highlightDirective{ @Input() public bcolor:string; // accessing value of this property outside from html. // when directive is called angular pass refrence of element in into below variable e constructor(private e:ElementRef){ console.log("Highlight directive constructor called"); // native element will give to original element hold by elementRef console.log("Element on which directive is applied is", e.nativeElement); var self = this; // changed the background color on which native element is applied e.nativeElement.style.backgroundColor = self.bcolor; e.nativeElement.style.color = "#fff"; e.nativeElement.style.border = "2px solid #ddd"; e.nativeElement.style.padding = "10px"; // changed the style only when mouse over on the HTML element this.e.nativeElement.onmouseover = function(){ // anonymous function self.e.nativeElement.style.backgroundColor = self.bcolor; e.nativeElement.style.border = "2px solid greeen"; } // changed the style only when mouse out on the HTML element e.nativeElement.onmouseout = function(){ // anonymous function e.nativeElement.style.backgroundColor = "#ccc"; e.nativeElement.style.border = "2px solid #ddd"; } // you can pass input parameter to the directive also and set dynamic color } }
That's it.By following above approach you can create the custom directive in angular2/4 easily.
If you have any query please contact on my skype Id:- rohit.oodles
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rohit Goyal
Rohit is an experienced Frontend Developer, having good experience in HTML5, CSS3, Bootstrap, Jquery, AngularJS, Angular4/5, Restful API Integration, ES6 . His hobbies are playing cards and cricket.