在选择框选项上使用ng-hide或ng-show

时间:2013-09-09 16:19:33

标签: javascript angularjs directive

是否可以使用ng-hide指令隐藏选择框选项?

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>

2 个答案:

答案 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;
        }
    });
}