【发布时间】:2019-07-15 00:05:00
【问题描述】:
我正在使用 angular6 multi-select,它有一个来自 ngOnInit 上的角度服务的对象数组中的项目列表,就像这样传递到 multi-select :
this.sensorTypes = [
{ label : "Power", value : "P"},
{ label : "Current", value : "C"},
{ label : "Voltage", value : "V"}
]
当表单加载时,我想在multi-select 中默认设置 2 个值。为此,我将ngModel 绑定到multi-select 并在该变量中设置ngOnInit 的值,就像这样
this.selectedAttributes = [
{label : "Current", value : "C"},
{label : "Voltage", value : "V"}
]
在我的 component.html 中,我正在像这样创建 multi-select:
<div class="form-group row">
<div class="col-sm-10">
<ng-select
[ngClass]="'ng-select'"
[(ngModel)]="selectedAttributes"
[ngModelOptions]="{standalone: true}"
[options]="sensorTypes"
[multiple]="true">
</ng-select>
</div>
</div>
但在多选中默认不设置值。
【问题讨论】:
-
你可以在
ngOnInit或任何类似的生命周期中做这样的事情:this.selectedAttributes = this.sensorTypes[0],如果sensorTypes是array的objects -
为什么我在绑定 ngModel 时需要这样做?
-
能否请您发布 StackBlitz
-
让我创建 stackBlitz
标签: angular typescript angular6 multi-select angular-ngselect