Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ng-hide or ng-show on select box option

Is it possible to hide select box options using the ng-hide directive?

http://jsfiddle.net/cr4UB/

<div ng-app ng-controller="Controller">
    <select ng-model="myDropDown">
          <option value="one">One</option>
          <option value="two" ng-hide="myDropDown=='one'">Two</option>
          <option value="three">Three</option>
    </select>

    {{myDropDown}}
</div>
like image 813
ThinkingInBits Avatar asked Sep 09 '13 16:09

ThinkingInBits


People also ask

Can we use ng-show and Ng-hide together?

Absolutely not. First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design.

What is difference between ng-show and Ng-hide?

The ng-show and ng-hide both are directive . The difference between is : ng-show directive will show the html element if expression resilts is true and ng-hide directive hide the html element if expression result is true .

How do you show ng-hide?

The ngHide directive shows or hides the given HTML element based on the expression provided to the ngHide attribute. The element is shown or hidden by removing or adding the . ng-hide CSS class onto the element.

What is the use of NG-hide?

Definition and Usage The ng-hide directive hides the HTML element if the expression evaluates to true. ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none .


2 Answers

AngularJS 1.1.5 has a directive ng-if which can work for you. Check this fiddle http://jsfiddle.net/cmyworld/bgsVw/

like image 77
Chandermani Avatar answered Sep 30 '22 15:09

Chandermani


I couldn't get it to work using ng-hide to check the value of your ng-model (likely some race-condition with reading/writing to the same model at once), however, I did come up with a working example of the functionality you're after:

View markup

<div ng-app ng-controller="Controller">
    <select ng-model="selectedOption" ng-options="o for o in options"></select>

    {{selectedOption}}
</div>

Controller

function Controller ($scope) {
    var initialOptions = ['One', 'Two', 'Three'];

    $scope.options = initialOptions;
    $scope.selectedOption = $scope.options[2]; // pick "Three" by default

    $scope.$watch('selectedOption', function( val ) {
        if( val === 'One' ) {
            $scope.options = ['One', 'Three'];
        } else {
            $scope.options = initialOptions;
        }
    });
}
like image 40
André Dion Avatar answered Sep 30 '22 15:09

André Dion