简体   繁体   English

隐藏ng-options中的选项

[英]hiding an option in ng-options

I'm pretty new to Angular and trying the ng-options. 我对Angular很新,并尝试了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. 根据视图,我想隐藏Read或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: 在我使用ng-options之前,我有一个正常的选择下拉并做了类似这样的事情:

<select>
    <option>Read Only </option>
    <option ng-show="shouldShowWrite">Write </option>
</select>

Is there a way to do this with ng-options? 有没有办法用ng-options做到这一点? Thanks. 谢谢。

You could use a filter in the ngOptions expression: 您可以在ngOptions表达式中使用过滤器:

<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: 并在控制器中将shouldShow()函数定义为$ scope:

$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 有关完整示例,请参阅: http//plnkr.co/edit/8FkVktDXKGg3MQQawZCH

Consider ngRepeat for that level of control. 考虑ngRepeat的控制级别。 I don't think it's possible with ngOptions. 我认为ngOptions不可能。

 <select ng-model="selectedValue">
    <option ng-repeat="permissionLevel in permissionLevels" value="{{permissionLevel.text}}" ng-show="shouldShowWrite(permissionLevel)">
      {{permissionLevel.text}}
    </option>
  </select>

Plaese check below code, it may help you to solve the problem!! Plaese检查下面的代码,它可以帮助您解决问题!

<select>
<option>Read Only </option>
<option ng-show="selectedValue.value=='ROLE_WRITE'">Write </option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM