2017-10-10 91 views
0

如果索引值匹配,我試圖動態設置selected屬性。與ngFor循環一起動態設置屬性

<select class="form-control" disabled> 
    <option *ngFor="let agency of request.agencyList" [attr.selected]="request.agencyIndex == agency">{{agency}}{{request.agencyIndex}} 
    </option> 
</select> 

對象:

this.requests = [{ 
     agencyList: ['Agency 1', 'Agency 2', 'Agency 3'], 
     agencyIndex: 1, 
     ... 
     }] 

然而,其將所有選項selected屬性false

+0

機構是一個字符串,agencyIndex是一個數字,我看到,request.agencyIndex ==機構 – Vega

回答

0

我認爲這是你在找什麼:

<select class="form-control"> 
    <option *ngFor="let agency of request.agencyList; let index=index;" [selected]="request.agencyIndex == index"> 
     {{agency}}{{request.agencyIndex}} 
    </option> 
</select>