简体   繁体   English

数据是否不在使用角度下拉列表中填充?

[英]Data is not populating in dropdown using angular?

I have datatable which will show the elements with edit button also top of the page i have add button . 我有数据表,其中将显示带有“编辑”按钮的元素,以及在页面顶部具有“添加”按钮的元素。

Add is for adding elements into list edit is for editing the particular row data Add用于将元素添加到列表中edit用于编辑特定的行数据

when user click add button one popup will come with two dropdownlist Element Name and Element symbol here based on Element Name selection Element symbol list will come. 当用户单击添加按钮时,将弹出一个带有两个下拉列表的元素名称和元素符号的下拉列表,此处基于元素名称选择。

My expectation > 我的期望>

when i click first row edit button(which means Hydrogen H ) same popup should open and we need to populate these Hydrogen(Element Name) and H(Element symbol) in dropdown like this 当我单击第一行编辑按钮(这意味着氢H)时,应打开相同的弹出窗口,我们需要像这样在下拉菜单中填充这些氢(元素名称)和H(元素符号)

when i click 2nd row edit buttin (Hydrogen ,H1) it should populate these data 当我单击第二行编辑buttin(氢,H1)时,应填充这些数据

here is my Dialogue html 这是我的对话html

<h1 mat-dialog-title>{{data.element.ModalName.name}}  </h1>
<form  [formGroup]="addTaskForm"  (ngSubmit)="save()" >
<mat-form-field>
  <mat-select formControlName="name" placeholder="Element Name"
  (selectionChange)="elementSelectionChange($event)">
    <mat-option *ngFor="let element of Elements" [value]="element.name">
      {{ element.name }}
    </mat-option>
  </mat-select>
</mat-form-field>
<mat-form-field>
  <mat-select  formControlName="symbol"  placeholder="Element symbol">
    <mat-option *ngFor="let element of selectedElementSymbols" [value]="element.symbol">
      {{ element.symbol }}
    </mat-option>
  </mat-select>
</mat-form-field>

<div mat-dialog-actions>

  <button mat-button (click)="onNoClick()">Cancel</button>
<button type="submit"  mat-button cdkFocusInitial>{{data.element.ModalName.button}}</button>



</div>
</form>

You need to add (click)="onEditClick()" event to HTML of your popup button. 您需要将(click)="onEditClick()"事件添加到弹出按钮的HTML中。 Then in TS file add onEditClick(){} and there you can catch data you have chosen in dropdowns 然后在TS文件中添加onEditClick(){} ,在那里您可以捕获在下拉菜单中选择的数据

store the selected table value to a variable like public 将所选表的值存储到变量如public

selected2 = this.options2[1].id ... selected2 = this.options2 [1] .id ...

and bind with mat-option 并与mat-option绑定

<mat-select [(ngModel)]="selected2">
    <mat-option *ngFor="let option of options2" [value]="option.id">{{ 
    option.name }}</mat-option>
  </mat-select>

https://stackblitz.com/edit/angular-talrct-prnwvr https://stackblitz.com/edit/angular-talrct-prnwvr

In above template I basically made two changes one on line 27 commented re initialization of element and in constructor to update list of symbols based on selection. 在上面的模板中,我基本上进行了两项更改,其中第27行注释了元素的重新初始化,并在构造函数中对基于选择的符号列表进行了更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM