是否可以使用ng-hide指令隐藏选择框选项?
<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>
答案 0 :(得分:18)
AngularJS 1.1.5有一个适用于您的指令ng-if
。检查这个小提琴http://jsfiddle.net/cmyworld/bgsVw/
答案 1 :(得分:2)
我无法使用ng-hide
检查ng-model
的值(可能是某些竞争条件同时读取/写入同一模型),但是,我做了提出你所追求的working example功能:
<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;
}
});
}