How to set default values in ng-select in angular6?
if you are using both bindlabel and bindvalue so fist find index of selected value t e
var index= this.sensorTypes.findIndex(x => x.ID ==something);
//this will set value
this.selectedAttributes= this.sensorTypes[index].ID;
You should use the [items]
input binding instead of [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 ngModel
to work.
import { NgSelectModule } from '@ng-select/ng-select';
import { FormsModule } from '@angular/forms';
.
.
@NgModule({
imports: [
FormsModule,
NgSelectModule,
.
.
.
values are not setting by default in multi-select
for this assign this.sensorTypes[0]
to ngModel of your ng-select
in ngOnInit()
ngOnInit() {
this.selectedAttributes = this.sensorTypes[0]
}
this will get the first attribute as the default one.