清除字段onButtonClick with setTimeout

时间:2018-01-16 00:03:30

标签: javascript angular angular5

我有两个字段:密码和ConfirmPassword, 单击按钮重置它们后,几秒钟后我需要清除该字段。

   <input tabindex="5" type="password" class="form-control" name="newPsw" [(ngModel)]="newPassword" (ngModelChange)=" checkPassword()">
  <input tabindex="5" type="password" class="form-control" name="confirmPsw" [(ngModel)]="confirmPsw" (ngModelChange)=" checkPassword()">

在组件上checkPassword函数:

       checkPassword() {
         //password validation here 
         //service that makes the change
         this.userService.changePassword(this.currentPassword, this.newPassword, this);

     }
  //response
 changePasswordResult(success: boolean) {
this.passwordUpdated = success;

}

如果密码已成功更新,我需要设置一个超时功能,在几秒钟后清除字段。 我把它放在:

  if(this.passwordUpdated = true) {
       setTimeout(function() {
        this.newPsw = ''
  },10000 )
}

这是一个好习惯吗?怎么办呢? 感谢。

1 个答案:

答案 0 :(得分:0)

我认为这是您需要的代码更改:

this.passwordUpdated = success;
if (success )
{
    setTimeout(function() {
        document.getElementsByName("newPsw")[0].value = "";
        document.getElementsByName("confirmPsw")[0].value = "";
  },10000 );
}