Html 角度8:为选择标记设置默认选定值

Html 角度8:为选择标记设置默认选定值,html,angular,select,option,ngfor,Html,Angular,Select,Option,Ngfor,我想为select标记设置一个默认选项值,这些值在我的组件中声明为表,并用ngFor指令显示: <form> <div class="form-group col-4"> <span class="badge badge-theme mb-3">Quantité</span> <select class="form-control" id="exampleFormControlSelect1">

我想为select标记设置一个默认选项值,这些值在我的组件中声明为表,并用ngFor指令显示:

<form>
    <div class="form-group col-4">
      <span class="badge badge-theme mb-3">Quantité</span>
      <select class="form-control" id="exampleFormControlSelect1">
        <option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
      </select>
    </div>
    <div class="form-group col-4">
      <span class="badge badge-theme mb-3">Message personnalisé</span>
      <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
    </div>
  </form>

您可以通过以下方式执行此操作:

component.html

 <select [(ngModel)]="seletedValue" class="form-control" id="exampleFormControlSelect1">
        <option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
 </select>

component.html
选择数量
{{数量}}
我已经创建了

我所做的是在Component中创建一个变量,然后将其与select标记绑定<代码>[(ngModel)]=“selectedValue”


然后可以根据您的逻辑使用变量。

您的选择将填充值,您无法设置默认值,是吗?是的,就是@PareshLomate
 <select [(ngModel)]="seletedValue" class="form-control" id="exampleFormControlSelect1">
        <option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
 </select>
seletedValue = ''; // set some default value from Quantities
component.html

 <select [(ngModel)]="seletedValue" class="form-control" id="exampleFormControlSelect1">
        <option value="" [disabled]="true">Select quantity</option>
        <option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
 </select>