【问题标题】:select angular doesn't show value correctly选择角度未正确显示值
【发布时间】:2020-06-20 02:30:20
【问题描述】:

我有一个带有数组的数组,它模拟了一本 csv 书:

[
  [ 'PROBANDO', 'SUBIENDO DESDE CSV', '1', '900', '', '2', '1' ],
  [ 'PROBANDO', 'SUBIENDO DESDE CSV', '1', '900', '850', '3', '1' ],
  [ 'PROBANDO', 'SUBIENDO DESDE CSV', '1', '900', '850', '2', '1' ]
]

我想要的是选择一个类别(索引 6)并在数组中更改它。

此代码完美地更改了类别,但我必须再次选择我选择的类别才能保持选中状态。

ts

  selectCat(event, idx: any) {
    let prod: any[] = this.productos[idx];

    prod.splice(6, 1, event.target.value); // 6 = posicion de la columna categoria

    this.productos.splice(idx, 1, prod);
  }

html

<tr *ngFor="let p of productos; index as idx">
    <td *ngFor="let i of p; index as ind">

        <p *ngIf="ind <= 4"> {{ i }} </p>

        <select *ngIf="ind == 6" name="cat" (change)="selectCat($event, idx)"
            class="form-control form-control-sm">
            <option *ngFor="let c of categorias" [value]="c.id"> {{ c.categoria }}
            </option>
        </select>

    </td>
</tr>

类别数组:

[
  {id: 2, categoria: "Cat22"},
  {id: 3, categoria: "cat3"},
  {id: 1, categoria: "Ropa"}
]

------------------------------------------ ---------------

我把选中的值放在select上面来验证是否正确

我做错了什么?

【问题讨论】:

  • 你能提供你的分类数组吗?
  • @SurajGupta 添加
  • 我已经创建了相同的演示,请看看link
  • @SurajGupta 是的,问题是 ngModel 丢失了!非常感谢

标签: html angular typescript


【解决方案1】:

您可以尝试使用[(ngModel)]="p[ind]" 代替(change)="selectCat($event, idx)"。我认为数据未正确绑定到选择输入。如果我理解正确,下拉菜单也应该在初始加载时显示“Ropa”。

【讨论】:

    猜你喜欢
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2012-11-05
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多