第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

角度和去抖動(dòng)

角度和去抖動(dòng)

紅顏莎娜 2019-08-12 10:45:05
角度和去抖動(dòng)在AngularJS中,我可以使用ng-model選項(xiàng)去抖模型。ng-model-options="{ debounce: 1000 }"如何在Angular中去抖模型?我試圖在文檔中搜索debounce,但我找不到任何東西。https://angular.io/search/#stq=debounce&stp=1一個(gè)解決方案是編寫(xiě)我自己的去抖函數(shù),例如:import {Component, Template, bootstrap} from 'angular2/angular2';// Annotation section@Component({   selector: 'my-app'})@Template({   url: 'app.html'})// Component controllerclass MyAppComponent {   constructor() {     this.firstName = 'Name';   }   changed($event, el){     console.log("changes", this.name, el.value);     this.name = el.value;   }   firstNameChanged($event, first){     if (this.timeoutId) window.clearTimeout(this.timeoutID);     this.timeoutID = window.setTimeout(() => {         this.firstName = first.value;     }, 250)   }}bootstrap(MyAppComponent);而我的HTML<input type=text [value]="firstName" #first (keyup)="firstNameChanged($event, first)">但是我正在尋找一個(gè)內(nèi)置函數(shù),Angular中有一個(gè)嗎?
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

它可以作為指令實(shí)施

import { Directive, Input, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';import { NgControl } from '@angular/forms';import 'rxjs/add/operator/debounceTime';import 'rxjs/add/operator/distinctUntilChanged';import { Subscription } from 'rxjs';@Directive({
  selector: '[ngModel][onDebounce]',})export class DebounceDirective implements OnInit, OnDestroy {
  @Output()
  public onDebounce = new EventEmitter<any>();

  @Input('debounce')
  public debounceTime: number = 300;

  private isFirstChange: boolean = true;
  private subscription: Subscription;

  constructor(public model: NgControl) {
  }

  ngOnInit() {
    this.subscription =
      this.model.valueChanges        .debounceTime(this.debounceTime)
        .distinctUntilChanged()
        .subscribe(modelValue => {
          if (this.isFirstChange) {
            this.isFirstChange = false;
          } else {
            this.onDebounce.emit(modelValue);
          }
        });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }}

用它就像

<input [(ngModel)]="value" (onDebounce)="doSomethingWhenModelIsChanged($event)">

組件樣本

import { Component } from "@angular/core";@Component({
  selector: 'app-sample',
  template: `
<input[(ngModel)]="value" (onDebounce)="doSomethingWhenModelIsChanged($event)">
<input[(ngModel)]="value" (onDebounce)="asyncDoSomethingWhenModelIsChanged($event)">
`})export class SampleComponent {
  value: string;

  doSomethingWhenModelIsChanged(value: string): void {
    console.log({ value });
  }

  async asyncDoSomethingWhenModelIsChanged(value: string): Promise<void> {
    return new Promise<void>(resolve => {
      setTimeout(() => {
        console.log('async', { value });
        resolve();
      }, 1000);
    });
  }}


查看完整回答
反對(duì) 回復(fù) 2019-08-12
  • 3 回答
  • 0 關(guān)注
  • 702 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)