1

我无法在角度 2 中为 md-select 设置值。我正在从构造函数填充选择列表,例如:

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

我可以使用 {{selectedValue}} 正确访问 html 中的这个值。但是该值未加载到选择字段上。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>

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

4

1 回答 1

1

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

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']);
    });
于 2017-03-02T14:08:07.403 回答