简体   繁体   中英

hiding an option in ng-options

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

Consider ngRepeat for that level of control. I don't think it's possible with 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!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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