Angular 默认选项选择的属性未按预期工作

标签 angular

我想从下拉列表中选择第一个选项。但是,当我设置 [attr.selected]="index == 1"[attr.selected]="index == 0" 它不起作用,但如果我将索引值条件更改为 [attr.selected]="index == 2",就可以了。尽管存在从 0 到 2 的索引。

<select formControlName="segmentforOverlapOne" class="form-control overlap-segment-1">
  <option [attr.selected]="index == 1" *ngFor="let segment of segmentations; let index=index;" [ngValue]="index">{{segment.name}}</option>
</select>

下面是分段数组:

[
  {
    "name": "name 1",
    "columns": [
      {
        "index": 0,
        ...
      }
    ],
  },
  {
    "name": "name 2",
    "columns": [
      {
        "index": 1,
        ...
      }
    ],
  },
  {
    "name": "name 3",
    "columns": [
      {
        "index": 2,
        ...
      }
    ]
  }
]

最佳答案

像这样尝试:

<select formControlName="segmentforOverlapOne" class="form-control overlap-segment-1"> 
    <option *ngFor="let segment of segmentations; let i = index" [attr.value]="i" [attr.selected]="i == 0 ? true : null">{{segment.name}}</option>
</select>

关于Angular 默认选项选择的属性未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47550394/

相关文章:

angular - 错误类型错误 : Cannot read property 'replace' of null

javascript - Angular - 在加载路由器之前获取路由器 url

javascript - Angular 中的数据管理 - 引用具有大量重复信息的类中的其他对象

node.js - Angular 形式组 patchValue 将值包裹在大括号中

angular - IE 11 Angular Material 复选框显示在同一位置

angular - 使用对非 ASP .net 应用程序不起作用的 URL 重写从响应中删除服务器 header 。

node.js - 接受来自其他网站的 POST 请求到 Angular 应用程序

angular - 如何获取父组件和子组件的表单值?

angular - 使用主机绑定(bind)设置自定义 CSS 变量不起作用

angular - 如何在 Angular 项目中显示保存在 wwwroot (asp.net core ) 中的图像