Angular2材质 - 无法将选定选项设置为md-select

时间:2017-04-19 06:44:55

标签: angular material-design angular-material

我正在尝试为客户端对象创建一个编辑表单, 首先,我从服务中获取数据,然后初始化表单

ngOnInit(){

    this.clientService.getCountries().subscribe(
        (result:any) => {
            this.countries = result.countries;
        }
    );

    this.subscription_route = this.route.params.subscribe(
        (params: any) =>{
            if(params.hasOwnProperty('id')){
                this.client_guid = params['id'];
                this.subscription_service = this.clientService.getClient(this.client_guid).subscribe(
                    (result:any) => {
                    this.client = result.client;
                        this.initForm();
                }
              );
            }
        }
    );

  }

我的initForm看起来像这样:

private initForm(){
        this.selectedOption = this.client.country_id;
    this.clientForm = this.formBuilder.group({
            name:[this.client.name,Validators.required],
            email:[this.client.email],
            vat_number:[this.client.vat_number],
            payment_terms_id:[this.client.payment_terms_id],
            address:[this.client.address],
            city:[this.client.city],
            postal:[this.client.postal],
            country_id:[this.client.country_id],
            accounting_key:[this.client.accounting_key],
            payment_amount:[this.client.payment_amount]
        });
  }

然后在我的HTML模板中,我有这个md-select

<md-select placeholder="country" formControlName="country_id">
    <md-option *ngFor="let country of countries" [value]="country.id">{{ country.name }}</md-option>
</md-select>

enter image description here

我无法设置从服务器获取的所选选项

2 个答案:

答案 0 :(得分:1)

我到处搜索并没有找到任何答案,所以我尝试使用属性和属性。 我注意到,当我没有使用* ng选择选项时,所以我认为问题出在[value]属性上 这种方式对我有用:

MyDlg dlg = new MyDlg ();
dlg.ShowDialog();

答案 1 :(得分:0)

如果您使用复杂的表单,可以执行类似

的操作
<md-select placeholder="country" [formControl]="clientForm.controls['country_id']">
    <md-option *ngFor="let country of countries" [value]="country.id">{{ country.name }}</md-option>
</md-select>

如果您更新表单值(如

),这将有效
clientForm.controls['country_id'].setValue(2);
例如

希望这有帮助。

亲切的问候克里斯

相关问题