Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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 更新mat select动态反应表单中的选项_Angular_Angular Reactive Forms - Fatal编程技术网

Angular 更新mat select动态反应表单中的选项

Angular 更新mat select动态反应表单中的选项,angular,angular-reactive-forms,Angular,Angular Reactive Forms,我想知道如何使用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

我想知道如何使用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>
但是selectedCargaId的值始终为空,从不更新

我需要将在父项中选择选项时从服务器检索的数据分配到子项中,我希望它使用mat select


再次感谢您的帮助。

我假设您有一个对象的
数组
,您需要从中列出短名单并显示在第二个mat中,根据第一个mat的选择选择选项

为此,您可以使用已回答的管道过滤选项

所以,在你的模板中类似这样的东西

<mat-option *ngFor="let opt of item.options | myfilter:cargaId" [value]="opt.key">

希望这有帮助!干杯

我假设您有一个
对象数组
,您需要从中列出短名单并显示在第二个mat中,根据第一个mat的选择选择选项

为此,您可以使用已回答的管道过滤选项

所以,在你的模板中类似这样的东西

<mat-option *ngFor="let opt of item.options | myfilter:cargaId" [value]="opt.key">

希望这有帮助!干杯

您好,谢谢您的回答,我在开始时没有对象数组,当我在第一次选择中选择某个选项时,我将cargaId传递给一个服务,该服务返回一个对象数组,其中包含第二次选择的数据,然后我需要用新数据替换第二次选择中的任何数据,但该更新适用于此选择实例,因为可能是我用相同的逻辑创建了另一个选择,因为它们是动态的。所以,如果您在cargeId更改时获得一组新数据,您可以将新返回的数组分配给第二个mat select,对吗?我不知道这里有什么困难。您可能只需要另一个专用于第二个mat select的数组,该数组由另一个服务更新。我不知道如何将新返回的数组设置为第二个select,我用管道尝试了您的答案,将新数组的值分配给空数组,但是返回错误。如果不查看您尝试的内容、代码和错误,就很难提供帮助。需要更多的细节和细节。您好,谢谢您的回答,我在开始时没有对象数组,当我在第一次选择中选择某个选项时,我将cargaId传递给服务,该服务返回一个对象数组,其中包含第二次选择的数据,然后我需要用新数据替换第二次选择中的任何数据,但这个更新是针对这个select实例的,因为我可能创建了另一个具有相同逻辑的select,因为它们是动态的。所以,如果您在cargeId更改时获得一组新数据,您可以将新返回的数组分配给第二个mat select,对吗?我不知道这里有什么困难。您可能只需要另一个专用于第二个mat select的数组,该数组由另一个服务更新。我不知道如何将新返回的数组设置为第二个select,我用管道尝试了您的答案,将新数组的值分配给空数组,但是返回错误。如果不查看您尝试的内容、代码和错误,就很难提供帮助。需要更多的细节和细节请。
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);
    }
}