Directives in Angular

 Directives


Directives are used to add behavior to elements in the DOM. Here is an example of a directive in Angular:


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


@Directive({

  selector: '[appHighlight]',

})

export class HighlightDirective {

  constructor(el: ElementRef) {

    el.nativeElement.style.backgroundColor = 'yellow';

  }

}

This directive sets the background color of any element with the appHighlight attribute to yellow.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...