Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在FormArray中显示MatSelect的默认选定值_Angular_Angular Material_Angular Reactive Forms - Fatal编程技术网

Angular 在FormArray中显示MatSelect的默认选定值

Angular 在FormArray中显示MatSelect的默认选定值,angular,angular-material,angular-reactive-forms,Angular,Angular Material,Angular Reactive Forms,我有一系列的选择字段,我试图显示默认的选择值,但运气不好。我的表单如下所示: <ng-container [formGroup]="Form"> <ng-container formArrayName="taxes" *ngFor="let item of taxes.controls; let i = index"> <div [formGroupName]="i" >

我有一系列的选择字段,我试图显示默认的选择值,但运气不好。我的表单如下所示:

<ng-container [formGroup]="Form">
           <ng-container formArrayName="taxes" *ngFor="let item of taxes.controls; let i = index">
                <div [formGroupName]="i" >
                    <mat-form-field>
                        <mat-select (selectionChange)="setTax($event.value, i)">
                            <mat-option *ngFor="let tax of taxesObjects" [value]="tax">
                                {{ tax.rate }}%
                            </mat-option>
                        </mat-select>
                    </mat-form-field>
                </div>
            </ng-container> 
        </ng-container>
 taxesObjects: SalesTaxModel[] = [
                {id: "1", name: "tax1", rate: 9},
                {id: "2", name: "tax2", rate: 12},
               ];
选择字段与任何
FormControl
都没有关联,我正在使用
setTax()
方法更新我的表单:

setTax(tax, index) {

        let taxAmount = parseFloat((tax.rate / 100 * this.preTaxValue).toFixed(2));

        this.taxes.at(index).get("amount").setValue(taxAmount)

        this.taxes.at(index).get("sales_tax").setValue(tax)
 }

当我为选择字段使用默认值时,我只是不知道如何在控件上显示所选值。有人能帮忙吗


这里有一个闪电战:

因为您使用mat选项值作为对象[value]=“tax”

必须将类似对象设置为“选择”的默认值。当您设置默认值时,它会通过检查相等来尝试将设置值与列表中的每个条目(每个mat选项)匹配,并且由于它是JSON对象,因此除非您进行深入检查,否则没有条目匹配

要解决此问题,可以使用mat select

// Template
<select [compareWith]="compareFn"  [formControl]="selectedCountriesControl">
    <option *ngFor="let country of countries" [ngValue]="country">
        {{country.name}}
    </option>
</select>

// component code
compareFn(c1: Country, c2: Country): boolean {
    return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
//模板
{{country.name}
//组件代码
compareFn(c1:国家,c2:国家):布尔值{
返回c1&&c2?c1.id==c2.id:c1==c2;
}

因为您使用mat选项值作为对象[value]=“tax”

必须将类似对象设置为“选择”的默认值。当您设置默认值时,它会通过检查相等来尝试将设置值与列表中的每个条目(每个mat选项)匹配,并且由于它是JSON对象,因此除非您进行深入检查,否则没有条目匹配

要解决此问题,可以使用mat select

// Template
<select [compareWith]="compareFn"  [formControl]="selectedCountriesControl">
    <option *ngFor="let country of countries" [ngValue]="country">
        {{country.name}}
    </option>
</select>

// component code
compareFn(c1: Country, c2: Country): boolean {
    return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
//模板
{{country.name}
//组件代码
compareFn(c1:国家,c2:国家):布尔值{
返回c1&&c2?c1.id==c2.id:c1==c2;
}

远离我的朋友。您正在寻找
[(ngModel)]
(ngModelChange)
。ngModel定义用于跟踪当前选择的变量,ngModelChange在选择更改时执行所需的任何计算。我应该如何在FormArray中使用
ngModel
?我不太明白…远离我的朋友。您正在寻找
[(ngModel)]
(ngModelChange)
。ngModel定义用于跟踪当前选择的变量,ngModelChange在选择更改时执行所需的任何计算。我应该如何在FormArray中使用
ngModel
?我不太明白这个。。。
// Template
<select [compareWith]="compareFn"  [formControl]="selectedCountriesControl">
    <option *ngFor="let country of countries" [ngValue]="country">
        {{country.name}}
    </option>
</select>

// component code
compareFn(c1: Country, c2: Country): boolean {
    return c1 && c2 ? c1.id === c2.id : c1 === c2;
}