且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角度 6 反应形式上的两种方式绑定

更新时间:2023-02-26 22:55:28

好吧,如果我理解正确的话,我遇到了类似的问题(我真的不知道这是否是***实践)但它对我有用,所以在 HTML 中:

Well if I understand you correctly I had a similar problem what I did (I really don't know if this is the best practice)but it's work for me so in the HTML:

<mat-form-field class="mat-container">
    <input matInput  [formControl]="generalDiscount" type="number" 
        formControlName="generalDiscount" 
        (input)="course.amounts.generalDiscount = $event.target.value" <-the workaround 
        placeholder="Discount" required="required">
</mat-form-field>

此输入使其成为双向绑定,并且在您的 .ts 类中,您需要将相同的字段放入表单组中,例如

This input makes it two way binding and in your .ts class you need to put the same field in your form group like

this.amountGroup = this._formBuilder.group({

    [this.course.amounts.fitToNomberOfPeople,Validators.required],
    generalDiscount:[this.course.amounts.generalDiscount,Validators.required],

});

希望能帮到你