角度7和角度材料如何获取mat-select的选定选项文本而不是其值

1990年

我需要获取所选材料下拉列表的文本<mat-select>而不是其值:

<ng-container matColumnDef="fr">
        <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" appearance="outline">
              <mat-label>Family Relation</mat-label>
              <mat-select #familyRelation (selectionChange)="onChange(element, i, 'hh')" 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>

这是我正在尝试做的事情:

@ViewChild('familyRelation') familyRel: ElementRef;

并在下拉列表的更改选择上:

onChange(data, i, type) {
    let c = this.familyRel.nativeElement.innerText;
    console.log(c)
}

我有以下错误:

错误TypeError:无法读取未定义的属性'innerText',位于

当我删除时innerText,控制台值是:

未定义

我需要什么,你可以在看stackblitz,如果我chosed Parent,我想Parent成为一个变量,而不是1这是它id

请注意elementihh中将(selectionChange)=onChange(element,...)在函数的后面使用,所以现在就算了吧。

罗希特007

更新了代码,并clickoptions

https://stackblitz.com/edit/angular-material-w89kwc?embed=1&file=app/app.component.ts

增加了一项功能

  getInnerText(innerText){
    console.log(innerText)
  }

在视图中添加了点击事件

<mat-option *ngFor="let familyRelation of familyRelationArray;" [value]="familyRelation.family_relation_id" (click)="getInnerText(familyRelation.family_relation_type)">
    {{familyRelation.family_relation_type}}
</mat-option>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章