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>
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.
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 .
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.
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 .
AngularJS 1.1.5 has a directive ng-if
which can work for you. Check this fiddle http://jsfiddle.net/cmyworld/bgsVw/
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:
<div ng-app ng-controller="Controller">
<select ng-model="selectedOption" ng-options="o for o in options"></select>
{{selectedOption}}
</div>
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;
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With