代码之家  ›  专栏  ›  技术社区  ›  alim1990

Angular 7 reactive form如何获取选定值的文本而不是下拉列表控件中的值

  •  1
  • alim1990  · 技术社区  · 6 年前

    我的剧本如下:

    <form [formGroup]="formGroup" formArrayName="test">
        <ng-container matColumnDef="fr" formArrayName="test">
                <th mat-header-cell *matHeaderCellDef> Family Rel. </th>
                <td mat-cell *matCellDef="let element; let i = index;">
                  <div [formGroupName]="i">
                    <mat-form-field color="warn" >
                        <mat-label>Family Relation</mat-label>
                        <mat-select (selectionChange)="onChange(element, i)" id="family_relation" formControlName="fr" placeholder="Family Relation">                 
                              <mat-option *ngFor="let familyRelation of familyRelationArray;" [value]="familyRelation.family_relation_id">
                            {{familyRelation.family_relation_type}}
                          </mat-option>
                        </mat-select>
                      </mat-form-field>&nbsp;
                    </div>
                </td>
              </ng-container>
    </form>
    

    (selectionChange="onChange(element, i)") ,我需要同时获取它的选定值和文本。

    onChange(data, i)
      {
        let group = (<FormArray>this.formGroup.get('test')).at(i).value;
        let newFr = group['fr'];
        console.log('Value: '+newFr);
      }
    

    其中数据是所选行,并且 i 是选定窗体数组的索引。

    我尝试使用旧的javascript方法:

    <HTMLSelectElement>document.getElementById().text
    

    [ts]属性'ttext '不存在于“HTMLMENTRONE”类型上。[2339]

    编辑

    我试过:

    @ViewChild('familyRelation') familyRelation;
    

    以及:

    <mat-select #familyRelation (selectionChange)="onChange(element, i)" id="family_relation" formControlName="fr" placeholder="Family Relation">                 
                          <mat-option *ngFor="let familyRelation of familyRelationArray;" [value]="familyRelation.family_relation_id">
                        {{familyRelation.family_relation_type}}
                      </mat-option>
                    </mat-select>
    

    enter image description here

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  2
  •   Sachila Ranawaka    6 年前

    首先,从表单控件获取值。然后通过循环数组获取与值相关的项。最后从项目中获取文本

    onChange(data, i)
      {
        let text = formGroup.get("fr").value;
        let newFr =this.familyRelationArray.find((item) => item.family_relation_id === text)
        if(newFr){
            console.log('text: '+newFr.family_relation_type); 
        }
      }
    
        2
  •  0
  •   Florian    6 年前

    根据您的评论,您需要一种访问html元素的通用方法。角度提供 easy way to do so

    <mat-select #selectFamilyRelation
                (selectionChange)="onChange()" 
                id="family_relation" 
                formControlName="fr" 
                placeholder="Family Relation">                 
      <mat-option *ngFor="let familyRelation of familyRelationArray;" 
                  [value]="familyRelation.family_relation_id">
        {{familyRelation.family_relation_type}}
      </mat-option>
    </mat-select>
    

    创建类属性: @ViewChild('selectFamilyRelation') familyRelation;

    然后要获取选定选项的文本:

    onChange() {
      const selectedOptionText = this.familyRelation._elementRef.nativeElement.innerText;
      // ...
    }