Angular 5 Dropdown值在启动时未显示为已选中

时间:2018-03-06 08:04:47

标签: angular

当我从服务器获取交换对象数组时,我遇到了Angular 5表单的问题,在迭代后我尝试填充HTML但默认值未显示,而其他字段从其他服务填充

这是我的HTML

<select formControlName="Exchange">
<option [value]="ExchangeName.Exchange" *ngFor='let ExchangeName of ExchangeNameAndPairsForDropDown'>{{ExchangeName.Exchange}}</option>
</select>  

这是我的TS代码

if (sourceExchangeArray && sourceExchangeArray.length) {
              for (var e = 0; e < sourceExchangeArray.length; e++) {
                this.sourceExchangeEmptyBox = (<FormArray>this.sourceTargetForm.get('sourceExchange')).push(
                  new FormGroup({
                    'Exchange' : new FormControl(sourceExchangeArray[e].Exchange , Validators.required),
                    'APIKey' : new FormControl(sourceExchangeArray[e].APIKey , Validators.required),
                    'NameInstance': new FormControl(sourceExchangeArray[e].NameInstance , Validators.required),
                    'Secret' : new FormControl(sourceExchangeArray[e].Secret , Validators.required),
                    '_id' : new FormControl(sourceExchangeArray[e]._id , Validators.required)
                  })
                );
              }
            }


getExchangeNameAndPairs () {
    this.ApplicationServices.getExchangeNameAndPairs('/api/exchanges/getExchangeNameAndPairs').then((success:any) => {
      if (success.ExchangeNameAndPairs && success.ExchangeNameAndPairs.length) {
        this.ExchangeNameAndPairsForDropDown = success.ExchangeNameAndPairs;
      }
      },
      error => {
        console.log('error', error);
      })

3 个答案:

答案 0 :(得分:0)

试试这个:

<select formControlName="Exchange">
<option *ngFor='let ExchangeName of ExchangeNameAndPairsForDropDown' [ngValue]="ExchangeName.Exchange">{{ExchangeName.Exchange}}</option>
</select> 

答案 1 :(得分:0)

值选项错误:[值] 它应该是:(值)

答案 2 :(得分:0)

对于任何面临此问题的人,您应该使用[ngValue]而不是[value]。