角度模式验证器停止调用onblur

时间:2019-03-05 23:55:31

标签: angular

我在角度5中使用Validator。验证不正确时会调用(blur)=“ update(false)”。如果输入值无效,如何停止onblur调用。

  <input required pattern="^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"
         id="url" (blur)="update(false)" type="text" class="form-control" name="url"
         [(ngModel)]="urls" #url="ngModel">
  <span class="help-block" *ngIf="url.errors?.required && url.touched">
    URL is required
  </span>
  <span class="help-block" *ngIf="url.errors?.pattern && url.touched">
    URL is Invalid
  </span>

1 个答案:

答案 0 :(得分:0)

尝试一下:

<input required pattern="^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"
         id="url" (blur)="url.invalid && update(false)" type="text" class="form-control" name="url"
         [(ngModel)]="urls" #url="ngModel">
  <span class="help-block" *ngIf="url.errors?.required && url.touched">
    URL is required
  </span>
  <span class="help-block" *ngIf="url.errors?.pattern && url.touched">
    URL is Invalid
  </span>