Bonci Marco
Bonci Marco

Reputation: 301

ng-select don't set value on reactive forms

I have a ng-select and angular reactive forms. I configured it:

this.form1 = this.fb.group({
  test: [null, null]
});


this.items = [
    {
     code: 'A',
     description: 'TEST A'
    },
    {
     code: 'B',
     description: 'TEST B'
    }

 ]

   <ng-select [items]="items" 
       (change)="onSelected($event)"
       bindLabel="description"
       class="selectbox"> 
    </ng-select>  


onSelected(event){
   this.form1.controls['test'].setValue(event);

   // this.form1.controls['test'].value =  {code: 'A',description: 'TEST A'}
}

Now the ng-select works. But if I set value on init i try to make

  this.form1.patchValue({
     test: {code: A}
  })

but don't worlks. I would like to set ui-select on init with this.form1.patchValue({ test: {code: A} })

any ideas? Thank's Marco

Upvotes: 1

Views: 6837

Answers (1)

Alua Kossamanova
Alua Kossamanova

Reputation: 41

bind formControlname to ng-select, and then on init set

this.form1.patchValue({ test: {code: A} }) or if your ng-select is set to [multiple]="true" then

this.form1.patchValue({ test: [{code: A}] })

Upvotes: 4

Related Questions