typescript 如何在angular6的ng-select中设置默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54800920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:48:04  来源:igfitidea点击:

How to set default values in ng-select in angular6?

angulartypescriptangular6multi-selectangular-ngselect

提问by Fahad Subzwari

I am using angular6 multi-selectwhich have a list of items coming in an array of objects from angular service on ngOnInitlike this which is passing into multi-select:

我正在使用 angular6 multi-select,它有一个项目列表,这些项目来自 angular 服务的对象数组,ngOnInit就像这样传入multi-select

this.sensorTypes = [
  { label : "Power", value : "P"},
  { label : "Current", value : "C"},
  { label : "Voltage", value : "V"}
]

I want to set 2 values by default in multi-selectwhen form will load. For this i am binding ngModelon multi-selectand in that variable i am setting values on ngOnInitlike this

我想在multi-select表单加载时默认设置 2 个值。对于这个我结合ngModelmulti-select,并在该变量我在设定值ngOnInit这样的

this.selectedAttributes = [
  {label : "Current", value : "C"},
  {label : "Voltage", value : "V"}
]

In my component.html i am creating multi-selectlike this :

在我的 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>

But values are not setting by default in multi-select.

但默认情况下,多选中并未设置值。

采纳答案by wentjun

You should use the [items]input binding instead of [options]

您应该使用[items]输入绑定而不是[options]

<ng-select 
  [items]="sensorTypes"
  bindLabel="label"                 
  [multiple]="true"
  placeholder="Select"
  [(ngModel)]="selectedAttributes">
</ng-select>

And on your component's module.ts, import the NgSelectModule. And if you haven't import your FormsModule, you should do so, as it needs to be imported for 2 way binding with ngModelto work.

在组件的 module.ts 上,导入NgSelectModule. 如果你还没有导入你的FormsModule,你应该这样做,因为它需要被导入以进行 2 路绑定ngModel才能工作。

import { NgSelectModule } from '@ng-select/ng-select';
import { FormsModule } from '@angular/forms';
.
.
@NgModule({
  imports: [
    FormsModule,
    NgSelectModule,
. 
.
.

回答by Manan bhavsar

if you are using both bindlabel and bindvalue so fist find index of selected value t e

如果您同时使用 bindlabel 和 bindvalue,那么首先找到所选值的索引 t e

var index= this.sensorTypes.findIndex(x => x.ID ==something); 
//this will set value
this.selectedAttributes= this.sensorTypes[index].ID;

回答by ganesh045

values are not setting by default in multi-select

多选中的默认值未设置

for this assign this.sensorTypes[0]to ngModel of your ng-selectin ngOnInit()

这个分配this.sensorTypes[0]给您的ngModelng-selectngOnInit()

ngOnInit() {
  this.selectedAttributes = this.sensorTypes[0]
}

this will get the first attribute as default one.

这将获得第一个属性作为默认属性。