import { bootstrapApplication } from '@angular/platform-browser';
import { Component, Directive, Input, HostBinding, HostListener } from '@angular/core';
import { CommonModule } from '@angular/common';
@Directive({
selector: '[w3Highlight]',
standalone: true
})
export class HighlightDirective {
@Input('w3Highlight') highlightColor = 'yellow';
@HostBinding('style.transition') transition = 'background-color 150ms ease-in-out';
@HostBinding('style.backgroundColor') bg = '';
@HostListener('mouseenter') onEnter() { this.bg = this.highlightColor; }
@HostListener('mouseleave') onLeave() { this.bg = ''; }
}
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, HighlightDirective],
styles: [`
.box { padding: 10px; border: 1px dashed #bbb; border-radius: 6px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
input { padding: 6px 8px; }
`],
template: `
<h3>Attribute Directive (highlight)</h3>
<div class="row">
<label>Color: <input [value]="color" (input)="color = $any($event.target).value" placeholder="e.g. lightyellow"></label>
<button (click)="toggle()">{{ enabled ? 'Disable' : 'Enable' }}</button>
</div>
<p style="margin-top:10px">Hover the boxes to see the effect:</p>
<div class="box" [w3Highlight]="color" *ngIf="enabled">I get highlighted on hover</div>
<div class="box">I do not</div>
`
})
export class App {
color = 'lightyellow';
enabled = true;
toggle() { this.enabled = !this.enabled; }
}
bootstrapApplication(App);
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Angular Directives - Attribute Directive</title>
</head>
<body>
<app-root></app-root>
</body>
</html>