我希望从数组中填充mat-checkbox。我能够建立表单组和表单数组,并在控制台中查看其内容。但是我无法使用ngFor获得要显示的设备列表。我查看了多个反应形式的示例,并认为我正在正确地执行此html,但是显然我缺少了一些东西。
这是ts代码:
import { Aircraft } from '../../shared/aircraft';
import { Component, OnInit, Inject } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, FormControl } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material";
import { ViewEncapsulation } from '@angular/core';
import { forEach } from '@angular/router/src/utils/collection';
interface IAircraftCheckbox {
imei: string;
selected: boolean;
name: string;
}
@Component({
selector: 'app-historical-track-dialog',
templateUrl: './historical-track-dialog.component.html',
styleUrls: ['./historical-track-dialog.component.scss',],
encapsulation: ViewEncapsulation.None
})
export class HistoricalTrackDialogComponent implements OnInit {
aircraftData: any[];
imeiToTrack: string[];
breakpoint: number;
form: FormGroup;
aircraftArray: FormArray;
allSelected: boolean;
constructor(public dialogRef: MatDialogRef<HistoricalTrackDialogComponent>,
private fb: FormBuilder,
@Inject(MAT_DIALOG_DATA) public data: Aircraft[]) {
this.aircraftData = data;
console.log('historical track dialog, this.aircraftList: ', data);
this.aircraftArray = new FormArray([new FormControl('CAP')]);
this.aircraftArray = this.mapAircraftToCheckboxArrayGroup( this.aircraftData);
this.form = new FormGroup({
aircraftList: this.aircraftArray
});
this.allSelected = false;
console.log('this.form: ', this.form);
console.log('this.aircraftList = ', this.form.controls.aircraftList);
}
mapAircraftToCheckboxArrayGroup( aircraft: Aircraft[]): FormArray {
return this.fb.array(aircraft.map((i) => {
return this.fb.group ({
imei: i.IMEI,
selected: false,
name: i.DeviceName
});
}));
}
checkboxToggle(e) {
console.log('CheckboxToggle, e=', e);
console.log('source.id', e.source.id);
console.log('status: ', e.checked);
}
toggleAll(e) {
for (const control of this.aircraftArray.controls) {
if (this.allSelected === true ) {
console.log('setting ', control.value.name, 'to false');
control.value.selected = false;
} else {
control.value.selected = true;
console.log('setting ', control.value.name, 'to true');
}
}
this.allSelected = this.allSelected === true ? false : true;
this.form.markAsDirty();
}
ngOnInit() {
this.breakpoint = (window.innerWidth <= 690) ? 1 : 4;
}
onResize(event) {
this.breakpoint = (event.target.innerWidth <= 690) ? 1 : 4;
}
close() {
console.log('close, formArray: ', this.aircraftArray.controls);
for (const control of this.aircraftArray.controls) {
if (control.value.selected === true ) {
console.log('aircraft checked: ', control.value.name);
}
}
this.dialogRef.close();
}
}
这是html:
<div class="aList-container"
fxLayout="column"
fxLayoutGap="10px"
fxLayoutAlign="start"
style="width:500px;max-height:50%">
<div >
<div class="a-list-title" >
<h3>AIRCRAFT LIST</h3>
<hr>
</div>
</div>
<div class="checkboxList" [formGroup]="form">
<mat-grid-list [cols]="breakpoint" rowHeight="30px" gutterSize="0px" formArrayName="aircraftList" (window:resize)="onResize($event)">
<mat-grid-tile fxLayout="column" *ngFor="let aircraftName of aircraftList.value" [formGroup]="aircraftList">
<mat-checkbox [formControl]="aircraftName.get('selected')">
<!--{{aircraftName.get('name').value}}-->
{{aircraftList}}
</mat-checkbox>
</mat-grid-tile>
<mat-grid-tile fxLayout="column" >
<mat-checkbox id="toggle-all" ng-model="all" (click)="toggleAll($event)">Toggle All</mat-checkbox>
</mat-grid-tile>
</mat-grid-list>
<div class="close-button">
<button class="mat-raised-button " routerLink="/home" (click)="close()" style="margin: 10px;position:center">Close</button>
</div>
</div>
</div>
在运行时,模板出现错误,告诉我未定义控件或值(均尝试)。但是当我记录formArray时,它们在那里:
我敢肯定我缺少一些简单而根本的反应式表格,但是现在已经追了三天了.....
答案 0 :(得分:0)
<mat-grid-tile fxLayout="column" *ngFor="let aircraftName of aircraftList.value" [formGroup]="aircraftList">
您尚未定义任何aircraftList
对象。它是表单组内的控件:
this.form = new FormGroup({ aircraftList: this.aircraftArray });
所以您也应该以这种方式访问它:
<mat-grid-tile fxLayout="column" *ngFor="let aircraftName of form.controls.aircraftList.value" [formGroup]="aircraftList">