在 ng-options 中隐藏一个选项 [英] hiding an option in ng-options
问题描述
我对 Angular 还是很陌生,正在尝试 ng-options.在我的控制器中,我有:
$scope.permissionLevels = [{ 值:ROLE_READ",文本:只读"},{ 值:ROLE_WRITE",文本:写入"}];
在我的模板中,我有:
根据视图,我想隐藏读取或写入.所以在我的控制器中,我有另一个标志来指示它是什么视图.在我使用 ng-options 之前,我有一个普通的选择下拉菜单,并做了这样的事情:
有没有办法用 ng-options 做到这一点?谢谢.
您可以在 ngOptions 表达式中使用过滤器:
并将 shouldShow() 函数定义为控制器中的 $scope :
$scope.shouldShow = function (permissionLevel) {//把你的授权逻辑放在这里返回 $scope.permission.canWrite ||permissionLevel.value !== 'ROLE_WRITE';}
有关完整示例,请参阅:http://plnkr.co/edit/8FkVktDXKGg3MQQawZCH>
I'm pretty new to Angular and trying the ng-options. In my controller, I have:
$scope.permissionLevels = [
{ value: "ROLE_READ", text: "Read Only" },
{ value: "ROLE_WRITE", text: "Write" }
];
In my template, I have:
<select ng-options="permissionLevel.text for permissionLevel in permissionLevels"
ng-model="selectedValue"></select>
Depending on the view, I want to hide either Read or Write. So in my controller, I have another flag that indicates what view it is. Before I used ng-options, I had a normal select drop down and did something like this:
<select>
<option>Read Only </option>
<option ng-show="shouldShowWrite">Write </option>
</select>
Is there a way to do this with ng-options? Thanks.
You could use a filter in the ngOptions expression:
<select ng-model="selectedValue"
ng-options="permissionLevel.text for permissionLevel in
permissionLevels | filter:shouldShow">
</select>
and define the shouldShow() function to $scope in the controller:
$scope.shouldShow = function (permissionLevel) {
// put your authorization logic here
return $scope.permission.canWrite || permissionLevel.value !== 'ROLE_WRITE';
}
For the full example see: http://plnkr.co/edit/8FkVktDXKGg3MQQawZCH
这篇关于在 ng-options 中隐藏一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!