angularjs 是什么原因导致此芯片列表在我的Angular 14和Angular material应用程序中失败?

niwlg2el  于 2023-02-07  发布在  Angular
关注(0)|答案(2)|浏览(154)

我正在做一个Angular 14的表格。
我正在尝试添加一个有Angular 材质cips字段。
在组件的Typescript文件中,我有:

constructor(private fb: FormBuilder,) { }

public visible: boolean = true;
public selectable: boolean = true;
public removable: boolean = true;
public addOnBlur: boolean = true;
public readonly separatorKeysCodes: number[] = [ENTER, COMMA];

public myForm: FormGroup = new FormGroup({
    companyName: new FormControl('', Validators.required),
    countryName: new FormControl('', Validators.required),

    bussinessLines: new FormGroup({
      bussinessLines: this.fb.array([], Validators.required)
    }),
});

get bussinessLineControls(): FormArray {
    return this.myForm.get('bussinessLines') as FormArray;
}

 public add(event: MatChipInputEvent): void {
    const input = event.input;
    const value = event.value;

    // Add our bussinessLine
    if ((value || "").trim()) {
      this.bussinessLineControls.push(this.fb.control(value));
    }

    // Reset the input value
    if (input) {
      input.value = "";
    }
}

public remove(bussinessLine: string): void {
    const index = this.bussinessLineControls.value.indexOf(bussinessLine);
    if (index >= 0) {
      this.bussinessLineControls.removeAt(index);
    }
}

在模板中:

<form [formGroup]="myForm" novalidate>
    <mat-form-field appearance="outline" floatLabel="always" class="example-chip-list">
        <mat-label>Bussiness lines:</mat-label>
        <mat-chip-list #chipList aria-label="Delect business line" formArrayName="bussinessLines">
            <mat-chip *ngFor="let bussinessLine of bussinessLineControls.value" [selectable]="selectable" [removable]="removable"
                (removed)="remove(bussinessLine)">
                {{bussinessLine}}
                <mat-icon matChipRemove *ngIf="removable">Cancel</mat-icon>
            </mat-chip>
            <input placeholder="New business line..."
                 [matChipInputFor]="chipList"
                 [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                 [matChipInputAddOnBlur]="addOnBlur"
                 (matChipInputTokenEnd)="add($event)">
        </mat-chip-list>
    </mat-form-field>
</form>

问题是

由于我无法理解的原因,控制台抛出错误:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables

它似乎发生在这里:

<input placeholder="New business line..."
  [matChipInputFor]="chipList"               
  [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
  [matChipInputAddOnBlur]="addOnBlur"
  (matChipInputTokenEnd)="add($event)">

问题

1.导致此错误的原因是什么?
1.最可靠的修复方法是什么?

jjjwad0x

jjjwad0x1#

发生错误的原因是您试图迭代对象(FormArray)而不是数组。"FormArray"类未实现Iterable接口。
若要修复此问题,可以使用FormArray的. controls属性,该属性返回其控件的数组。因此,在模板中,将以下内容替换为:

<mat-chip *ngFor="let bussinessLine of bussinessLineControls.value" [selectable]="selectable" [removable]="removable" (removed)="remove(bussinessLine)">
    {{bussinessLine}}
    <mat-icon matChipRemove *ngIf="removable">Cancel</mat-icon>
</mat-chip>

与:

<mat-chip *ngFor="let bussinessLine of bussinessLineControls.controls; let i = index" [selectable]="selectable" [removable]="removable" (removed)="remove(i)" >
    {{bussinessLine.value}}
    <mat-icon matChipRemove *ngIf="removable">Cancel</mat-icon>
</mat-chip>

在remove函数中,将其更改为:

public remove(index: number): void {
    this.bussinessLineControls.removeAt(index);
}
velaa5lx

velaa5lx2#

嗯......它是白纸黑字写的,或者更像是红色和红色:)您将object传递到了 *ngFor指令的某个地方。您没有提供足够的代码来查找bug,至少我无法找到。您是否可以证明更大的代码块?至少包括<mat-chip-grid #chipList>mat-chip-row *ngFor="let var of list"?因为您组件中唯一的可迭代对象是:separatorKeysCodesbussinessLineControls吸气剂。
如果我蒙着眼睛射击,我会假设您忘记将controls属性放入*ngFor

// This is you'r code for *ngFor <mat-chip-row>
*ngFor="let businessLineControl of bussinessLineControls"

// This is how it should have been
*ngFor="let businessLineControl of bussinessLineControls.controls"

相关问题