对于每一次按键,它都会在数据库中搜索

时间:2019-12-11 04:57:43

标签: angular typescript

HTML代码

<input id="email type="email" (keypress)="checkIfEmailExistFunction($event)">

TS代码

async checkIfEmailExistFunction(e) {
    const email = e.key;
    if (email.length < 0 &&this.registerForm.controls.Email.errors ) {
      await this.submissionService.checkEmailExist({ email });
    };
}

有没有办法让我搜索用户每次按键的数据库?

1 个答案:

答案 0 :(得分:0)

您可以使用双向绑定来实现此目的。初始化变量'searchTerm'并使用ngModel将其绑定到输入

HTML代码

<input id="email type="email"  [(ngModel)]="searchTerm" (ngModelChange)="checkIfEmailExistFunction(searchTerm)">

TS代码

searchTerm: string = "";

  async checkIfEmailExistFunction(searchTerm) {
    const email = searchTerm;
    if (email.length < 0 &&this.registerForm.controls.Email.errors ) {
      await this.submissionService.checkEmailExist({ email });
    };
}

您可以像这样通过HTML传递搜索词,也可以不使用参数直接使用serchterm

async checkIfEmailExistFunction() {
    const email = this.searchTerm;
    if (email.length < 0 &&this.registerForm.controls.Email.errors ) {
      await this.submissionService.checkEmailExist({ email });
    };
}