Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Angular FAQ: Top Questions

35. What are custom directives and how to create them?

Custom directives are user-defined Angular directives used to extend HTML behavior.


@Directive({ selector: '[highlight]' })
export class HighlightDirective {
  constructor(el: ElementRef) {
    el.nativeElement.style.backgroundColor = 'yellow';
  }
}
        
  • Attach with: <div highlight></div>