首页 > 解决方案 > Angular 8 选择的选项编号和字母

问题描述

我选择的选项有问题

when the name_step = "ABCD" is selected but

当未选择 name_step = "1" 时。

为什么会发生这种情况,即使我将两者都设置为字符串

这是我的代码:

<option *ngFor="let step of listStep | keyvalue" value={{step.value.id}}  selected="{{step.value.name === name_step ? 'selected' : '' }}">
          {{ step.value.name | titlecase }}</option>

我有 3 个选项

option 1 [id = 1, name = "1"],
option 2 [id = 2, name = "2"],
option 3 [id = 3, name = "ABCD"]

这个代号 name_step :

this.name_step = params['params']['name_step'];

标签: angular

解决方案


现场演示

我不确定我的问题是否正确,但您可以问这样的问题吗

<mat-form-field>
  <mat-label>Cars</mat-label>
  <select matNativeControl required>
   <option *ngFor="let step of listStep " value={{step.id}}  selected="{{step.name == name_step ? 'selected' : '' }}">
          {{ step.name | titlecase }}</option>
  </select>
</mat-form-field>

ts文件代码

name_step='ABCD';

  listStep: any[] = [
    { id: 1, name: "1" },
    { id: 2, name: "2" },
    { id: 3, name: "ABCD" }
  ];

推荐阅读