Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 角度/如何将获取的数据从字符串转换为数组_Angular_Typescript - Fatal编程技术网

Angular 角度/如何将获取的数据从字符串转换为数组

Angular 角度/如何将获取的数据从字符串转换为数组,angular,typescript,Angular,Typescript,我在对话框中编辑了表单,但我正在接收的select的api数据是字符串而不是数组,因此我得到以下错误: 错误:值必须是多选模式下的数组。 campaign.ts ngOnInit(): void { this.fetchdata(); } fetchdata() { this.campaignService.sendGetRequest() .subscribe((data: any[])=>{ this.campaigns = data;

我在对话框中编辑了表单,但我正在接收的select的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;
})
编辑.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>
忽略不匹配的行(无声失败)


忽略不匹配的行(无声失败)

在javascript中有一个函数split(separator),它通过分隔符将字符串拆分为数组

因此,在您的情况下,当您构建formGroup时,您将设置,例如:


Branch:[this.data.Branch.split(',')],
在javascript中有一个函数split(separator),它通过分隔符将字符串拆分成数组

因此,在您的情况下,当您构建formGroup时,您将设置,例如:


Branch:[this.data.Branch.split(“,”)],

嘿,谢谢你的回答,很有效!还有一个问题,有时某些数据将为空,因此我会遇到以下错误:无法读取nullQuick解决方案的属性“split”:(this.data.branch | |“”)。split(','))或更长,并且仅处理会使this.data.branch为空!==无效的this.data.branch.split(','):[]谢谢!!!!!!嘿,谢谢你的回答,很有效!还有一个问题,有时某些数据将为空,因此我会遇到以下错误:无法读取nullQuick解决方案的属性“split”:(this.data.branch | |“”)。split(','))或更长,并且仅处理会使this.data.branch为空!==无效的this.data.branch.split(','):[]谢谢!!!!!!