【问题标题】:How can I setvalue a select options in Angular 7?如何在 Angular 7 中设置选择选项的值?
【发布时间】:2019-04-28 07:51:12
【问题描述】:

问题是当我想更改值时,它总是向我显示第一个选项。

这是我的html:

 <div class="col-lg-4 col-md-4 col-sm-12 mt-4">
                    <div class="form-group">
                        <select class="form-control" id="operator-groups" formControlName="operators">
                            <option disabled selected value>Seleccione</option>
                            <option *ngFor="let operator of operators; let i = index;">{{operator.desc}}</option>
                        </select>
                    </div>
                </div>

这是我的组件:

this._operatorgroups.index().subscribe(res => {
      let operators = <OperatorGroups[]>res;
      this.operators  = operators;
      this.registerForm.controls['operators'].setValue(res[1].desc);
    });

谢谢!

【问题讨论】:

    标签: javascript html angular angular7


    【解决方案1】:

    您可以使用[selected]="condition that will result true for the option you want default"

    例如,如果你想要第一个索引(基于你在组件中的代码),你会喜欢:

    <option *ngFor="let operator of operators; let i = index;" [selected] = "i==1">{{operator.desc}}</option>
    

    另一个例子是,如果你想选择 operator desc 'xyz',你会喜欢:

     <option *ngFor="let operator of operators; let i = index;" [selected] = "operator.desc == xyz">{{operator.desc}}</option>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-19
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      相关资源
      最近更新 更多