I have followed all of the steps mentioned on Creating a Custom Debounce Click Directive in Angular and tried to use this custom directive for a hyperlink as shown below:
directive.ts:
import { Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output }
from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit, OnDestroy {
@Input() debounceTime = 500;
@Output() debounceClick = new EventEmitter();
private clicks = new Subject();
private subscription: Subscription;
constructor() { }
ngOnInit() {
this.subscription = this.clicks.pipe(
debounceTime(this.debounceTime)
).subscribe(e => this.debounceClick.emit(e));
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
@HostListener('click', ['$event'])
clickEvent(event) {
event.preventDefault();
event.stopPropagation();
this.clicks.next(event);
}
}
.html:
<a appDebounceClick (debounceClick)="delete()" [debounceTime]="700"></a>
I also make necessary import definitions in app.module.ts and my-component.ts. But when debugging it I encounter "Can't bind to 'debounceTime' since it isn't a known property of 'a'" error. Do I need to define a custom click event in the directive? If so how?
Can't bind to 'debounceTime' since it isn't a known property of 'a'means the[debounceTime]input you apply on your link isn't recognized. Seems like your code osn't the issue, so please provide a minimal reproducible example of it.@Inputsyntax such as[debounceTime]="500"