3

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.

4
  • Use ng-disabled and use model value of one of the dropdowns to disable/enable the other. Docs: docs.angularjs.org/api/ng/directive/ngDisabled Commented Dec 15, 2015 at 18:40
  • What does your second dropdown look like? Does it have a ng-disabled with the expression you wish to disable on? Commented Dec 15, 2015 at 18:44
  • This is under a loop and i am dynamically generating the dropdowns.. so the same expression for ng-disabled will be applied to all of them. @scareddragon how can i use the model value of the dropdowns when they are under the loop Commented Dec 15, 2015 at 19:02
  • @MonishDas I've added an answer with example and JSFiddle. Hope that helps. Commented Dec 15, 2015 at 20:58

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.