【问题标题】:angular bind to a table of select tags - how to get the value?角度绑定到选择标签表 - 如何获取值?
【发布时间】:2019-05-16 02:38:39
【问题描述】:

我有一个表,其中每个表行都有一个选择标签,并且很快意识到我不能使用 ngModel,至少不能像您将它用于单个选择那样。

有没有使用 angular 绑定到每个选择,并让用户选择选项/值?

谢谢

<tr *ngFor="let item of owner; let i = index">
<td>{{i + 1}}</td>
<td>
    <select [(ngModel)]="selectedId" class="form-control">
        <option *ngFor="let brand of brands" [value]="brand.Id">
        {{ brand.name | titlecase }}</option>
    </select>
</td>

【问题讨论】:

    标签: html angular angular5


    【解决方案1】:

    为了达到预期的效果,将索引值附加到 ngModel 以区分每个下拉菜单,而不是对所有下拉菜单使用相同的 ngModel

    <tr *ngFor="let item of owner; let i = index">
      <td>{{i + 1}}</td>
      <td>
        <select [(ngModel)]="selectedId[i]" class="form-control">
          <option *ngFor="let brand of brands" [value]="brand.Id">
            {{ brand.name | titlecase }}</option
          >
        </select>
      </td>
    </tr>
    

    代码示例 - https://codesandbox.io/s/wwnxyjrnr8

    【讨论】:

      【解决方案2】:

      像这样使用 [selected] 属性,

      {{ 品牌名称 |标题大写 }}

      而不是 [ngModel]。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        • 1970-01-01
        • 1970-01-01
        • 2019-04-17
        相关资源
        最近更新 更多