【问题标题】:NgModel empty with select html tagNgModel 为空,带有选择 html 标记
【发布时间】:2017-10-10 08:15:42
【问题描述】:

看看这个:

        <label>Business unit</label>
        <select name="BU" class="form-control" [(ngModel)]="interventionForm.DLCODBUN">
            <option *ngFor="let item of buList" >{{item.id}}</option>
        </select>

当我打开相关网页时,选择框会显示一个默认值(列表的第一个),但这只是一个视图绑定。

here's the image

事实上,如果我在应用程序中继续,与 [(ngModel)] 绑定的变量将是未定义的。

只有在选择框上选择interventionForm.DLCODBUN 才会被填充,但我想要默认值相同的东西! 不奇怪吗?有人知道解决方法吗?

【问题讨论】:

    标签: html angular angular-ngmodel ngfor


    【解决方案1】:

    使用您的 component.ts 中的默认值分配 interventionForm.DLCODBUN 。然后它也会出现在选择框中,并且您将在 component.ts 中获得值。在您从下拉更改某些内容之前,您不会获得分配的值,因此最好的选择是在 component.ts 中已经分配一个默认值

    【讨论】:

      【解决方案2】:

      可能是option标签中缺少ngValue属性造成的。

      尝试在选项标签中添加项目值,它可能会起作用。

      <option *ngFor="let item of buList" [ngValue]="item.value">{{item.id}}</option>
      

      【讨论】:

      • "dat do de ting"。谢谢XD
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多