Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

[Angular Fundamental] Build Angular Directives #29

Open
reboottime opened this issue Nov 22, 2023 · 4 comments
Open

[Angular Fundamental] Build Angular Directives #29

reboottime opened this issue Nov 22, 2023 · 4 comments

Comments

@reboottime
Copy link
Owner

reboottime commented Nov 22, 2023

Overview

This article discusses Angular directives, covering:


References

@reboottime
Copy link
Owner Author

reboottime commented Nov 22, 2023

What is Angular Directive

Usage and classifications

Directive is an instruction to the DOM, we use directive to

  • manipulate DOM
  • change behavior
  • Add/Remove DOM elements

With angular directives, we can encapsulate reusable behaviors— directives can apply attributes, CSS classes, and event listeners to an element.



Angular Directive Types

image

  • Component directive: is the angular component, it is a directive with a template
  • Attribute directives: manipulate DOM attributes. It is used to change the appearance of a DOME element
    • built in directives: ngClass and ngStyle
  • structure directives: change the DOM layout, adding/removing an element from page, its pattern

structural directives are applied they generally are prefixed by an asterisk, *, such as *ngIf


@reboottime
Copy link
Owner Author

reboottime commented Nov 22, 2023

How to build Angular directives

Attribute directive

A step by step guide on building a highlight directive.

Angular supports generate directive using cli, bellow is the command to generate a directvi

sh ng generate directive highlight


  • Step 1: Define the highlight directive
import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  @Input() highlightColor: string = 'yellow';

  constructor(private el: ElementRef) {}

  @HostListener('mouseenter') onMouseEnter() {
    this.highlight(this.highlightColor || 'yellow');
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.highlight(null);
  }

  private highlight(color: string | null) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

  • Step 2: Import the directive to AppModule

After importing the directive at the AppModule, you can utilize it everywhere inside of the app

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HighlightDirective } from './highlight.directive';

@NgModule({
  declarations: [
    AppComponent,
    HighlightDirective // Add the directive here
  ],
  imports: [BrowserModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

  • Step 3:Apply a directive somewhere
<div [appHighlight]="'cyan'">
  Hover over me to highlight in cyan.
</div>
<div [appHighlight]="'pink'">
  Hover over me to highlight in pink.
</div>

@reboottime
Copy link
Owner Author

reboottime commented Nov 22, 2023

How to build Angular directives

Structure directive

@reboottime
Copy link
Owner Author

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant