Since ngModel is updating instantly how to put a delay.
<input type="text" value="{{item.task_name}}" name="task_name" [(ngModel)]="item.task_name" (ngModelChange)="update_fields([item.task_name])" >
I need to save the task_name with a delay of one seconds by calling update_fields() , To avoid instant calls to service.
Thanks
Add delay inside your
update_fields()
method.Like:
Rxjs and Observables are the perfect candidate for this type of task! Here is an example of how it can be achieved:
Template:
Component:
subject
is a type of object that acts both as an observable and observer - meaning you can both subscribe to it and emit values from it (withnext()
)!debounceTime
waits for the provided time in ms until it allows for new changesdistinctUntilChanges
will not allow the same input to pass through two times in a rowswitchMap
takes the latest observable from the chain so you don't get multiple results at onceHere's a solution that works with a callback.
view template:
component class: