【发布时间】:2019-07-03 04:48:14
【问题描述】:
我想知道如何使用 Angular 7 中的动态表单更新动态创建的 mat-select 中的选项。它具有以下结构:
<span *ngIf="item !== undefined">
<div [formGroup]="form">
<div [ngSwitch]="item.controlType" class="col s12 m12 l4">
<mat-form-field *ngSwitchCase="'textbox'" class="example-full-width">
<input matInput [placeholder]="item.label" [formControlName]="item.key" [id]="item.key" [type]="item.type">
</mat-form-field>
<mat-form-field *ngSwitchCase="'dropdown'">
<mat-select [formControlName]="item.key" [placeholder]="item.label" *ngIf="item.callback==1"
(selectionChange)="cargaDropDown(item.origenCallBack, item.key);">
<mat-option *ngFor="let opt of item.options" [value]="opt.key">
{{opt.value}}
</mat-option>
</mat-select>
<mat-select [formControlName]="item.key" [placeholder]="item.label" *ngIf="item.callback==0">
<mat-option *ngFor="let opt of item.options" [value]="opt.key">
{{opt.value}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="errorMessage" *ngIf="!isValid">{{item.label}} is required</div>
</div>
</span>
我有一种 mat-select,它有回调,应该根据用户的选择填充其他 mat-select,我只知道要更新选项的 mat-select 的表单控件名称,我需要知道如何实现这一点,我在执行 cargaDropDown 方法时已经收到数据,谢谢您的帮助。
更新:
我按照角度页面中的教程进行操作:
https://angular.io/guide/dynamic-form
我动态创建了表单,我有两种选择一种是父母,另一种是孩子,我试图实现你的答案,只是为了将新数据的值分配给项目:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'myfilter',
pure: false
})
export class MyFilterPipe implements PipeTransform {
transform(items: any[], selectedCargaId: number): any {
if (!items || !selectedCargaId) {
return items;
}
// filter items array, items which match and return true will be
// kept, false will be filtered out
return items.filter(item => item.options.cargaId === selectedCargaId);
}
}
但 selectedCargaId 的值始终为 null,从不更新。
我需要将在父亲中选择选项时从服务器检索的数据分配给孩子,我希望它与 mat-select 一起使用。
再次感谢您的帮助。
【问题讨论】:
标签: angular angular-reactive-forms