Angularjs disabling drop down

我与影子孤独终老i 提交于 2019-12-08 21:49:28

Use ng-disabled and use model value of one of the dropdowns to disable/enable the other.

Example app:

angular.module('app', []).controller('MyController', function($scope){
  $scope.dropdownSelections = {};

  $scope.dropdownA = [
    {value: 1, label: 'Item A1'},
    {value: 2, label: 'Item A2'},
    {value: 3, label: 'Item A3'},
    {value: 4, label: 'Item A4'}
  ];

  $scope.dropdownB = [
    {value: 1, label: 'Item B1'},
    {value: 2, label: 'Item B2'},
    {value: 3, label: 'Item B3'},
    {value: 4, label: 'Item B4'}
  ];

  $scope.dropdownC = [
    {value: 1, label: 'Item C1'},
    {value: 2, label: 'Item C2'},
    {value: 3, label: 'Item C3'},
    {value: 4, label: 'Item C4'}
  ];
});

Example template code:

<div ng-controller="MyController">
  <div>Dropdown selections: {{dropdownSelections}}</div>
  <div>
    <legend>Dropdown A</legend>
    <select name="A" id="A" ng-model="dropdownSelections.dropdowA" ng-options="item.value as item.label for item in dropdownA"></select>
  </div>
  <div>
    <legend>Dropdown B</legend>
    <select name="B" id="B" ng-model="dropdownSelections.dropdowB" ng-options="item.value as item.label for item in dropdownB" ng-disabled="dropdownSelections.dropdowA !== 2"></select>
  </div>
  <div>
    <legend>Dropdown C</legend>
    <select name="C" id="C" ng-model="dropdownSelections.dropdowC" ng-options="item.value as item.label for item in dropdownC" ng-disabled="dropdownSelections.dropdowB !== 3"></select>
  </div>
</div>

DropdownB will be enabled when DropdownA has option 2 selected. DropdownC will be enabled when DropdownB has option 3 selected. Of course this is only basic example, the code is not perfect, but demonstrates the idea.

I've created working example in this JSFiddle.

More information about ng-disabled can be found in this doc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!