我在表单中嵌套了select元素,我只希望每个值选择一次。如何检查occupation.occupation
数组中是否存在selectedoccupations
并将[disabled]=''
设置为true(如果存在)?
成分</ P>
selectedOccupations: any[] = [
{ occupation: 'dev' },
];
模板
<tr>
<td>
<md-select formControlName="occupation" [ngModelOptions]="{standalone: true}" placeholder="Occupation" (change)="filterGuests($event, i)">
<md-option [value]="null">Occupation</md-option>
<md-option *ngFor="let occupation of occupationz" [value]="occupation.occupation" [disabled]="">
{{ occupation.occupation }}
</md-option>
</md-select>
</td>
</tr>
答案 0 :(得分:2)
调用typescript函数,为你返回值!
<tr>
<td>
<md-select formControlName="occupation" [ngModelOptions]="{standalone: true}" placeholder="Occupation" (change)="filterGuests($event, i)">
<md-option [value]="null">Occupation</md-option>
<md-option *ngFor="let occupation of occupationz" [value]="occupation.occupation" [disabled]="isDisabled(occupation)">
{{ occupation.occupation }}
</md-option>
</md-select>
</td>
</tr>
然后在打字稿中:
function isDisabled(occupation: any):boolean{
// if it exists... disabled == true;
return selectedOccupations.includes(occupation.occupation); // note, i'm not certain if this should just be occupation.
}
答案 1 :(得分:0)
试试[disabled] = "selectedOccupations.includes(occupation)"
?
这不是很有效,但不应该那么糟糕。