ng-select请勿在反应式表单上设置值

时间:2019-05-16 13:05:48

标签: angular angular-reactive-forms reactive-forms angular-ngselect

我有一个ng-select和角度反应形式。我配置了它:

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'}
}

现在ng-select起作用了。但是如果我在init上设置值,我会尝试

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

但不要担心。我想使用this.form1.patchValue({          测试:{code:A}       })

有什么想法吗?谢谢 马可

1 个答案:

答案 0 :(得分:0)

将formControlname绑定到ng-select,然后在初始设置

this.form1.patchValue({ test: {code: A} }),或者如果您的ng-select设置为[multiple] =“ true”,那么

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