我在对话框中有编辑表单,但是要接收的用于选择的api数据是字符串形式而不是数组形式,所以出现此错误:
错误:值必须是多选模式下的数组。
campaign.ts
ngOnInit(): void {
this.fetchdata();
}
fetchdata() {
this.campaignService.sendGetRequest()
.subscribe((data: any[])=>{
this.campaigns = data;
})
}
onEdit(item){
this.campaigns.findIndex(i => i.id == item.id);
const dialogRef = this.dialog.open(EditComponent,{
data: item
});
}
编辑
this.editForm = this.fb.group({
Name: this.data.name,
SmsText: [this.data.smsText, [Validators.required]],
SmsSendDate: [this.data.smsSendDate, [Validators.required]],
SmsSendHoursRange: this.fb.group({
start: [this.data.smsSendHoursRange.start, [Validators.required]],
end: [this.data.smsSendHoursRange.end, [Validators.required]],
}),
Branch: [this.data.branch],
InspectionResult: [this.data.inspectionResult],
Categories: [this.data.categories],
DateOfIssue: [this.data.dateOfIssue],
Brand: [this.data.brand],
VehicleCategory: [this.data.vehicleCategory],
OwnerStatus: [this.data.ownerStatus],
IsPhys: [this.data.isPhys],
IsJRDC: [this.data.isJRDC]
});
// get centers
this.campaignService.getCenters().subscribe((data: any[])=>{
this.centers = data;
})
// get vehicles
this.campaignService.getCategories().subscribe((data: any[])=>{
this.categories = data;
})
edit.html
<div class="column">
<mat-form-field appearance="fill">
<mat-label>Car category</mat-label>
<mat-select multiple formControlName="Categories">
<mat-option *ngFor="let item of categories" [value]="item.id">{{item.description}}</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="columns">
<mat-form-field appearance="fill">
<mat-label>Centers</mat-label>
<mat-select multiple formControlName="Branch">
<mat-option *ngFor="let item of centers" [value]="item.centerCode">{{item.address}}</mat-option>
</mat-select>
</mat-form-field>
</div>
这是我想在数组中进行转换以供选择的api响应
branch: "G25,G22"
brand: "OPEL, SUBARU"
inspectionResult: "1"
...............
在javascript中,有一个split(separator)函数,该函数通过分隔符将字符串分割成数组。
因此,在您的情况下,当构造formGroup时,您将进行设置,例如:
Branch: [this.data.branch.split(',')],
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句