带有 select html 标签的 NgModel 为空
NgModel empty with select html tag
看这个:
<label>Business unit</label>
<select name="BU" class="form-control" [(ngModel)]="interventionForm.DLCODBUN">
<option *ngFor="let item of buList" >{{item.id}}</option>
</select>
当我打开相关网页时,select 框显示默认值(列表的第一个),但这只是一个视图绑定。
here's the image
事实上,如果我在应用程序中继续,与 [(ngModel)] 绑定的变量将是未定义的。
只有在 select 框 interventionForm.DLCODBUN 上有一个 selection 才会被填充,但我想要同样的东西和默认值!
不奇怪吗?有人知道解决方法吗?
将 interventionForm.DLCODBUN 分配给 component.ts 中的默认值。然后它也会出现在 select 框中,您将在 component.ts 中获得值。在从下拉列表中更改某些内容之前,您不会获得分配的值,因此最好的选择是分配一个已经在 component.ts
中的默认值
可能是option标签中缺少ngValue属性造成的
尝试将项目值添加到选项标签中,它可能会起作用。
<option *ngFor="let item of buList" [ngValue]="item.value">{{item.id}}</option>
看这个:
<label>Business unit</label>
<select name="BU" class="form-control" [(ngModel)]="interventionForm.DLCODBUN">
<option *ngFor="let item of buList" >{{item.id}}</option>
</select>
当我打开相关网页时,select 框显示默认值(列表的第一个),但这只是一个视图绑定。
here's the image
事实上,如果我在应用程序中继续,与 [(ngModel)] 绑定的变量将是未定义的。
只有在 select 框 interventionForm.DLCODBUN 上有一个 selection 才会被填充,但我想要同样的东西和默认值! 不奇怪吗?有人知道解决方法吗?
将 interventionForm.DLCODBUN 分配给 component.ts 中的默认值。然后它也会出现在 select 框中,您将在 component.ts 中获得值。在从下拉列表中更改某些内容之前,您不会获得分配的值,因此最好的选择是分配一个已经在 component.ts
中的默认值可能是option标签中缺少ngValue属性造成的
尝试将项目值添加到选项标签中,它可能会起作用。
<option *ngFor="let item of buList" [ngValue]="item.value">{{item.id}}</option>