加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2:尽管选择了“selected”属性,但选择下拉列表不选择选

发布时间:2020-12-17 17:54:32 所属栏目:安全 来源:网络整理
导读:我有一个选择下拉列表的以下代码: select id="UnitOfMeasurementId" name="UnitOfMeasurementId" [(ngModel)]="UnitOfMeasurementId" option *ngFor="let unit of UnitOfMeasurements" [ngValue]="unit.Value" [selected]="unit.Selected"{{unit.Text}}/opt
我有一个选择下拉列表的以下代码:

<select id="UnitOfMeasurementId" name="UnitOfMeasurementId" [(ngModel)]="UnitOfMeasurementId">
    <option *ngFor="let unit of UnitOfMeasurements" [ngValue]="unit.Value" [selected]="unit.Selected">{{unit.Text}}</option>
</select>

UnitOfMeasurements数组中的每个项看起来像这样:

Selected: false
Text: "lb"
Value: "1"

或这个:

Selected: true
Text: "kg"
Value: "3"

[(ngModel)] =“UnitOfMeasurementId”包含应选择的项的值.在此特定示例中,该值为3,因此应选择第3个项目.果然,当我检查元素时,它在正确的项目上显示ng-reflect-selected =“true”,但实际上没有选择任何内容.如何才能获得列表中的正确项目以实际动态选择而不是仅添加ng-reflect-selected =“true”属性?

解决方法

不要将选定的属性与ngModel和ngValue一起使用,而是将所选项的值分配给UnitOfMeasurementId.

重要的是它具有与* ngFor中使用的实例相同的实例.具有相同属性和相同值的其他一些对象实例将不起作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读