Deboune one or more native events in a angular 2 app with a directive.
$ npm install ng2-debounce --save
- Add the "ng2-debounce" directive to a native dom element.
- Bind to "ng2dEvent" the event/s to debounce.
- Listen to "ng2dOnEvent".
<input
type="text"
ng2-debounce
[ng2dEvent]="'keyup'"
(ng2dOnEvent)="myHandler($event)"
>
This is a list of parameters for the directive:
Name | Type | Required | Default | Description |
---|---|---|---|---|
ng2dEvent | string | string[] | yes | This should be set first and represent the event or group or events that should be debounced. | |
ng2dDelay | number | no | 3000 | Delay of the event in miliseconds. |
ng2dOnEvent | function($event: any) => void | yes | A handler that will get all the info from the event. |
Debounce the "keyup" event and call "myHandler" when it is done.
<input
type="text"
ng2-debounce
[ng2dEvent]="'keyup'"
(ng2dOnEvent)="myHandler($event)"
>
Debounce "keyup" and "change" events and call "myHandler" when any of this is done.
<input
type="number"
ng2-debounce
[ng2dEvent]="['keyup', 'change']"
(ng2dOnEvent)="myHandler($event)"
>
Change the delay to execute "myHandler" to 1000 miliseconds. Works with multiple events.
<input
type="text"
ng2-debounce
[ng2dEvent]="'keyup'"
[ng2dDelay]="1000"
(ng2dOnEvent)="myHandler($event)"
>
MIT © Alva Damián