45
loading...
This website collects cookies to deliver better user experience
ng generate directive <directive-name>
ng g d <directive-name>
my-underline
ng g d my-underline
my-underline.directive.ts
.import { Directive } from '@angular/core';
@Directive({
selector: '[appMyUnderline]'
})
export class MyUnderlineDirective {
constructor() { }
}
@Directive
. The directive's selector is appMyUnderline
. In order to use the directive we need to use the selector name.constructor(private el: ElementRef, private renderer: Renderer2) {
this.renderer.setStyle(this.el.nativeElement,
'textDecoration', 'underline');
}
ElementRef
or Renderer2
(which I will discuss in the advanced concept section).app.component.html
file and paste in the below code -<div appMyUnderline>This text will be underlined!</div>