我正在使用选择框发送数据。句子部分只有管理员和管理员才能发送数据。例如,只有管理员和管理员可以选择一个和两个。普通用户无法选择它。仅用于reference。
HTML
<select [(ngModel)]="selectedOption" name="gender">
<option value="A">All</option><br>
<option *ngFor="let option of selectOptions" [value]="option">
{{option}}
</option>
</select>
<p>
{{ "selectedOption: " + selectedOption }}
</p>
组件
this.userRole = localStorage.getItem('role');
public selectOptions = [
"One",
"Two",
"Three"
];
}
答案 0 :(得分:1)
您可以在demo上找到它,也许对您有帮助!
课程文件
userRole: string = 'manager'; // localStorage.getItem('role');
模板文件
您可以在disabled
中attribute
option
,当用户不是管理员或管理员并且选项值为一或两个禁用该选项时
<select [(ngModel)]="selectedOption" name="gender">
<option value="A">All</option><br>
<option *ngFor="let option of selectOptions"
[value]="option"
[disabled]="!(userRole == 'admin' || userRole == 'manager') && (option === 'One' || option === 'Two')"
>{{option}}
</option>
</select>
<p>
{{ "selectedOption: " + selectedOption }}
</p>