【问题标题】:Angular 6: Not able to get the selected value on selectionChange eventAngular 6:无法在 selectionChange 事件中获取选定的值
【发布时间】:2020-04-09 09:59:46
【问题描述】:

在我的编辑表单中,我无法检索在添加时存储的值。 流程就像选择第一个选择框的一个选项后,它的相关数据将显示在下一个选择框中。

我已经为选定的值尝试了这个answer,但没有显示这些值。

我的表格如下:

<form [formGroup]="productForm" (ngSubmit)="onSubmit()">
    <mat-form-field">
      <mat-label>Main Type</mat-label>
      <mat-select [(value)]="selectedType" (selectionChange)="onTypeChange($event.value)"
        formControlName="mainTypeId">
        <mat-option *ngFor="let list of mainList" [value]="list.id">{{list.name}}</mat-option>
      </mat-select>
    </mat-form-field>
    <mat-form-field">
      <mat-label>Sides Type</mat-label>
      <mat-select [(value)]="selectedSide" formControlName="sideTypeId">
        <mat-option *ngFor="let list of sidesList" [value]="list.id">{{list.name}}
        </mat-option>
      </mat-select>
    </mat-form-field>
</form>

EditProductComponent.ts

export class EditProductUploadImageComponent implements OnInit {
    public selectedType: any;
  public selectedSide: any;

  constructor(private fb: FormBuilder,
    private productService: ProductService) {
    this.formInitialization();
    this.getSelectedData()
  }

    getSelectedData() {
    this.productService.getProductDesignRef(this.data.editId).subscribe((response: any) => {
        this.refrences = response.data[0]
        console.log(this.refrences)
        this.productService.getMainListById(this.refrences.main_type_id).subscribe((response: any) => {
          this.selectedType = response.data[0].id
          this.getMainTypeList()
          if(response) {
            this.productService.getSidesListById(this.refrences.sides_type_id).subscribe((response: any) => {
              this.selectedSide = response.data[0].id
              this.onTypeChange(1)
            })
          }
        })
    })
  }

  getMainTypeList() {
    this.productService.getMainTypeList().subscribe((response: any) => {
      if (response) {
        this.mainList = response.data;
        console.log(this.mainList)
      }
    }
  }

  onTypeChange(value: number) {
    this.productService.getSidesTypeListById(value).subscribe((response: any) => {
      if (response) {
        this.mainTypeListById = response['data']['0']['sides-type'];
        this.sidesList = this.mainTypeListById
        console.log(this.sidesList)
      }
    }
  }
}

=> 通过使用参考 ID,我在表单中传递了一个选定的值。但无法获得正确的输出。

请查看此jsfiddle link 中的所有控制台日志

如需更多说明,请查看这两张关于工作流程的图片。

  1. 记录列表:https://prnt.sc/rw2ucu;单击编辑按钮时,将打开一个编辑窗口,我希望列表页面中列出的两个值用于更新记录。
  2. 编辑对话框:https://prnt.sc/rw2xeh

提前谢谢...!!!

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    我刚刚注意到您正在使用响应式表单。

    <!-- why are you using value and formControlName & why you passing $event.value in your method? -->
    <mat-select 
       [(value)]="selectedType" 
       (selectionChange)="onTypeChange($event.value)"
       formControlName="mainTypeId"
    >
    
    <!-- when you are using reactive forms you can just access mainTypeId in your onTypeChange method -->
    <mat-select 
       (selectionChange)="onTypeChange()"
       formControlName="mainTypeId"
    >
    
    // your method
    onTypeChange() {
       // your access mainTypeId here directly like this
       const mainTypeId = this.yourFormName.get('mainTypeId').value;
    }
    

    如果上述方法不适用于您的编辑案例,您可以使用 [compareWith] 轻松处理您的编辑案例。

    如果你没有使用反应式:

    1. 第一件事是我看不到你的任何价值 onTypeChange 方法。
    2. 在这里 (selectionChange)="onTypeChange($event.value)" 你不必 传递 $event.value 因为对于您已经在 selectedType 中设置的值
    3. 您只需要在 onTypeChange 方法中访问 this.selectedType

    如果不清楚,请发表评论,我会尽快回复您。

    【讨论】:

    • 不工作先生......我想知道为什么没有任何解决方案有效。我在stackblitz.com/edit/angular-p9ruf4 上创建了这个;也许你可以更好地了解流程。
    • 太好了,我会在 30 分钟或更短时间内回复您
    • 谢谢先生。有关更多说明,请查看这两个有关工作流程的图像。 1) 记录列表:prnt.sc/rw2ucu;单击编辑按钮时,将打开一个编辑窗口,我希望列表页面中列出的两个值用于更新记录。 2) 编辑对话框:prnt.sc/rw2xeh
    • 您分享的 stackblitz 链接。该应用程序似乎已损坏。您应该设置最小的代码结构。我可以与您通话以解决您的问题,您可以共享您的屏幕,我可以帮助您找到问题。
    • 它现在正在工作,先生,正如您上面的答案......!!!我再次检查了这些步骤并解决了对象键名的更改。万分感谢。干杯。
    【解决方案2】:

    您不能在同一个材质组件上使用 [(value)] 和 formControlName。它不正常。

    并在 getSelectedData 函数中将 this.selectedType = response.data[0].name 更新为 this.selectedType = response.data[0].id

    您没有在函数 onTypeChange

    中使用选定的值
    onTypeChange(value: number) {
      this.productService.getSidesTypeListById(1).subscribe((response: any) => {
         if (response) {
           this.mainTypeListById = response['data']['0']['sides-type'];
           this.sidesList = this.mainTypeListById
           console.log(this.sidesList)
         }
       }
    }
    

    您必须使用 价值 才能看到影响。

    【讨论】:

    • 在我的实际代码中,我已经传递了一个 value 参数,当我点击第一个选择时它会产生影响......但这里我的要求是当我加载编辑时那时的表格应该检索附加值。
    • 你是在某处设置这个“selectedType”吗?
    • 您正在分配字符串,而在 HTML 中您正在使用 id 作为值。
    • 把 this.selectedType = response.data[0].name 改成 this.selectedType = response.data[0].id 或者你可以创建一个stackblitz
    • 您好,先生,这是一个 stackblitz 链接:stackblitz.com/edit/angular-p9ruf4 PS:我没有了解如何更新 stackblitz 中的 api 调用,还删除了旧的 cmets,以便线程不能被超过。
    猜你喜欢
    • 2019-10-05
    • 2011-01-28
    • 1970-01-01
    • 2020-06-29
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多