簡體   English   中英

默認情況下,選擇標簽選擇一個值Angular Typescript

[英]By Default Select tag one value selected Angular Typescript

我希望默認選擇一個值。

這是stackblitz鏈接: https ://stackblitz.com/edit/angular-lwculd

HTML部分:

<select [(ngModel)]="selected" (ngModelChange)="onSelect(selected)" class="form-control">

        <option selected="Select" *ngFor="let status of statuses" [value]="status">
          {{ status }}
        </option>
      </select>

TS部分:

 statuses = ['Select', 'Enable', 'Temporary Disable', 'Permanent Disable', 'Lock User', 'Auto Disable'];

  onSelect(val) {
    console.log(val);
  }

在您的app.component.ts ,只需使用您要默認selected的值來初始化selected即可:

  statuses = ['Select', 'Enable', 'Temporary Disable', 'Permanent Disable', 'Lock User', 'Auto Disable'];
  selected = this.statuses[0]; // <-- add this line
  // You could also write : selected = 'Select';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM