Angular 4, How to update [(ngModel)] with a delay of 1 seconds

前端 未结 6 1556
攒了一身酷
攒了一身酷 2021-02-05 16:17

Since ngModel is updating instantly how to put a delay.

  

        
6条回答
  •  长情又很酷
    2021-02-05 16:45

    Here's a solution that works with a callback.

    view template:

    
    

    component class:

        actionsByElements = new Map();
    
        delayAction(element: HTMLElement, cb: Function, args: any[]) {
          // cancel countdown by element
          let action = this.actionsByElements.get(element);
          if(action) {
            action.unsubscribe();
          }
    
          // start new countdown by element
          action = timer(1000).subscribe(() => cb.apply(this, args));
          this.actionsByElements.set(element, action);
        }
    
        doSomething(){...}
    

提交回复
热议问题