当accommodation
的索引0
时,将选定的attribute
添加到选项中。我不确定我做错了什么。
以下是我正在使用的代码
<select formControlName="type" required>
<option value="{{ accommodation.id }}" *ngFor="let accommodation of ListAccommodationTypes; let i = index;" ng-selected="0 in i">{{ accommodation.name }}</option>
</select>
这就是它在 DOM 中呈现的内容
<select formcontrolname="type" required="" ng-reflect-name="type" class="ng-untouched ng-pristine ng-invalid">
<!--template bindings={
"ng-reflect-ng-for-of": "[object Object],[object Object],[object Object],[object Object],[object Object]"
}-->
<option ng-selected="0 in i" value="4" ng-reflect-value="4">Something 4</option>
<option ng-selected="0 in i" value="5" ng-reflect-value="5">Something 5</option>
<option ng-selected="0 in i" value="3" ng-reflect-value="3">Something 3</option>
<option ng-selected="0 in i" value="2" ng-reflect-value="2">Something 2</option>
<option ng-selected="0 in i" value="1" ng-reflect-value="1">Something 1</option>
</select>
我也试过
<select formControlName="type" required>
<option value="{{ accommodation.id }}" *ngFor="let accommodation of ListAccommodationTypes; let i = index;" ng-selected="i==0">{{ accommodation.name }}</option>
</select>
<select formControlName="type" required>
<option value="{{ accommodation.id }}" *ngFor="let accommodation of ListAccommodationTypes; let i = index;" ng-selected="{{i==0}}">{{ accommodation.name }}</option>
</select>
试试这个
<option *ngFor="let accommodation of ListAccommodationTypes; let i = index;"
[attr.selected]="i == 0 ? true : null">
{{ accommodation.name }}
</option>