双击自动填充表单字段,但不符合要求的验证程序的条件?

问题描述 投票:-1回答:2

我有以下表单组:

this.order = new FormGroup({
    City: new FormControl('', Validators.required),
    Street: new FormControl('', Validators.required),
    DateOfDelivery: new FormControl('', Validators.required),
    CreditCard: new FormControl('', [Validators.required,/*Validators.pattern('^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$')*/])
})

和以下component.html

<form [formGroup]="order">
    <label>City</label>
    <br>
    <input class="form-control" list="City" name="City" formControlName="City" placeholder="Choose City" (dblclick)='dblClickCity($event.target)'>
    <datalist id="City">
        <option value="New York">
        <option value="Berlin">
        <option value="London">
    </datalist>
    <br>
    <label>Street</label>
    <br>
    <input type="text" class="form-control" placeholder="Please Input Street for Delivery"
           formControlName="Street" (dblclick)='dblClickStreet($event.target)'>
    <br>
    <label>Shipping Date</label>
    <br>
    <input type="date" class="form-control" placeholder="First Name here" formControlName="DateOfDelivery">
    <br>
    <label>Credit Card:</label>
    <br>
    <input type="text" class="form-control" placeholder="Credit Card here" formControlName="CreditCard">
    <br>
</form>

双击城市或街道表单控件时,将调用以下功能,并使用用户数据自动填写输入:

dblClickCity(target) {
    console.log(target.touched);
    target.value = this.city;
}
dblClickStreet(target) {
    target.value = this.street;
}

但是,页面底部的确认按钮状态保持禁用([disabled] ='!order.valid'),仅当我在每个字段上手动击键时才有效。

任何想法如何解决这个问题?

提前感谢。

angular autofill angular-custom-validators
2个回答
0
投票

想通了。必须手动使用.setValue方法并将其输入到双击处理程序中,如下所示:

  dblClickCity() {
    this.order.controls.City.setValue(this.city);
  }

  dblClickStreet() {
    this.order.controls.Street.setValue(this.street);
  }


0
投票

供以后参考,如果要在不创建特定功能的情况下动态设置值,可以执行以下操作:

 dblClickHandler(event) {
    const control = event.target.getAttribute('formControlName');
    this.order.get(control).setValue(event.target.value);
 }
© www.soinside.com 2019 - 2024. All rights reserved.