How to set default values in ng-select in angular6?
Asked Answered
V

4

20

I am using angular6 multi-select which have a list of items coming in an array of objects from angular service on ngOnInit like this which is passing into 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-select when form will load. For this i am binding ngModel on multi-selectand in that variable i am setting values on ngOnInit like this

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

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

<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.

Venule answered 21/2, 2019 at 6:52 Comment(4)
you can do something like this in ngOnInit or any of life cycle like this : this.selectedAttributes = this.sensorTypes[0], if sensorTypes is array of objectsCarthy
why i need to do this when i am binding ngModel having values in it?Venule
can you please post StackBlitzCarthy
let me create stackBlitzVenule
D
11

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,
. 
.
.
Dizen answered 21/2, 2019 at 6:56 Comment(8)
but the list shows correctly in angular multi select when i open drop down. then only problem is that i cna't set default values dynamically.Venule
Hmm.. But according to the ng-select documentations (npmjs.com/package/@ng-select/ng-select), there is not such binding for [options]. I think you are required to use [items] in order for it to workDizen
I have edited my solution; added bindLabel. Do you wanna try copying and pasting my code and see if it works?Dizen
yes it is throwing an error like this Can't bind to 'items' since it isn't a known property of 'ng-select'.Venule
Oh.. Are you using an older version of ng-select?Dizen
i am using this version "ng-select": "^1.0.1"Venule
Ok, did you import { NgSelectModule } from '@ng-select/ng-select'; and import { FormsModule } from '@angular/forms' onto your component module?Dizen
Let us continue this discussion in chat.Venule
N
4

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;
Nash answered 25/2, 2020 at 12:13 Comment(1)
This is the answer helped me, since i'm using both bind value and bind label. For people who don't understand -> You have to bind the value you given as bindValue as the value, not the bindLabel valueTimberlake
E
3

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.

Escolar answered 21/2, 2019 at 7:18 Comment(1)
what if i have to set multiple values in ng-select by defailt?Venule
C
0

There are two different ng-select modules:

  • @ng-select/ng-select
  • ng-select

both directive tags are same and [options] used in ng-select and [items] used in @ng-select/ng-select

ng-select documentation https://basvandenberg.github.io/ng-select/#/documentation

Its all your choice to use any of one

Cushitic answered 30/9, 2021 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.