Angularjs disabling drop down

一笑奈何 提交于 2019-12-23 01:40:06

问题


I have a ui in which based on the selection of one drop down another dropdown should be disabled. Both these dropdowns are generated using ng-repeat . Below is the code sample

<tr data-ng-repeat="abc in xyz.divisionViewList" >
<select
                 data-ng-model="abc.selectedAppRole"
                 ng-init="abc.selectedAppRole =abc.selectedAdRole"
                 id="{{abc.applicationId}}_{{abc.divisionId}}" name="{{abc.selectedAppRole}}">
                 <option value="null">Select/Deselect a role</option>
                <option data-ng-repeat="roleDetail in abc.roleDetails" data-ng-selected="{{abc.adRole == abc.selectedAdRole}}"
                 value="{{abc.adRole}}">&nbsp;{{abc.roleDesc}}&nbsp;</option>
                </select>
</tr>

As this is a dynamic generated drop downs based on ng- repeat, i want to put validations based on selection on one drop down. Please let me know how can i put this validation so that i can disable and enable any dropdown based on selection of the other. I am really stuck on this.


回答1:


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.



来源:https://stackoverflow.com/questions/34296853/angularjs-disabling-drop-down

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