This command will create a directive TypeScript file and add it to our app module so it can be used in the application.
Inside the directive TypeScript file, there is a @Directive decorator on top of our class declaration to indicate that this class is a directive.
<p appCustomDirective>This is a very long text.</p>
Save the project and start the local server. Open the browser and go to localhost:4200.
getElementById().style.color = "red";
To change the color of the text, we take the reference, access the style property, and then set the color to red.
this.htmlElement.style.color = "red"
this.htmlElement.addEventListener();
import { Component, HostListener } from '@angular/core';
@HostListener("click")
Now elements that has this attribute will listen for any click events and change the text color to orange.
import { Component, HostListener, Input } from '@angular/core';
Then declare an input variable and instead of using orange, we will use the value of the input.
@Input() color: string;
@HostListener("click")
<p appCustomDirective [color]="purple">This is a very long text.</p>
<p [appCustomDirective]="purple">This is a very long text.</p>
This is called the compact syntax.
@Input() appCustomAttribute: string;
Save the project and go to the browser.
Recap