2017-03-02 88 views
1

我不能为设定值MD-选择在角2.我从构造函数中填充的选择列表,如:无法设置为默认值MD-选择在角2

constructor() { 
     this.testService.getTypes() 
     .subscribe(result => { 
      if (result.code === 200) { 
       this.types = result.data;       
      } 
     }, error => { 
      this.router.navigate(['signin']); 
     }); 
} 

其工作正常,数据在md-select上正确填充。然后我运行一些查询后设置上ngOnInit()的值,并设置类似的值:

this.selectedValue = 'value'; 

我能正确使用HTML访问此值{{了selectedValue}}。 但是该值未加载到选择字段上。 MD-选择代码是:

<md-select placeholder="Type" [(ngModel)]="selectedValue" [formControl]="form.controls['typeName']" [required]="isRequired" style="width: 100%"> 
     <md-option *ngFor="let type of types" [value]="type.typeId" [disabled]="type.disabled"> 
      {{ type.typeName }} 
    </md-option> 

任何帮助,将不胜感激。提前致谢!

+0

如果您有formControl,则不需要ngModel。它将直接从formControl获取值。 –

回答

1

你没有发布完整的代码,但我会尽力帮你没有它。首先,如果你有一个formControl,你不需要ngModel。它基本上是做同样的事情 - 两种数据绑定。

this.testService.getTypes() 
    .subscribe(result => { 
     if (result.code === 200) { 
      this.types = result.data; 
      this.form.controls['typeName'].setValue(this.types[0].typeId); //add this line      
     } 
    }, error => { 
     this.router.navigate(['signin']); 
    }); 
+0

感谢您的回答! –