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.

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 objects Commented Feb 21, 2019 at 6:57
  • why i need to do this when i am binding ngModel having values in it? Commented Feb 21, 2019 at 7:02
  • can you please post StackBlitz Commented Feb 21, 2019 at 7:06
  • let me create stackBlitz Commented Feb 21, 2019 at 7:13

4 Answers 4

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,
. 
.
.
Sign up to request clarification or add additional context in comments.

8 Comments

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.
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 work
I have edited my solution; added bindLabel. Do you wanna try copying and pasting my code and see if it works?
yes it is throwing an error like this Can't bind to 'items' since it isn't a known property of 'ng-select'.
Oh.. Are you using an older version of ng-select?
|
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;

1 Comment

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

1 Comment

what if i have to set multiple values in ng-select by defailt?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.