Ashane Alvis Ashane Alvis - 5 months ago 31
AngularJS Question

How to enable disable a select drop down list using radio buttons with angularjs?

I have been searching all over for the internet looking on how to enable or disable this drop down list using radio buttons specifically when the radio button value is equal to prof the drop down list should be disabled, but with no help. I did come up with an example but didn't work. Any help would be appreciated.

registration.html

<div class="form-group">
<label class="col-lg-2 col-md-3 control-label">Qualification</label>
<div class="col-lg-10 col-md-9">
<div class="radio-custom radio-inline">
<input type="radio" ng-model="QualificationDetails.qualification_type" value="edu" name="radio1" id="radio4">
<label for="radio4">Educational</label>
</div>
<div class="radio-custom radio-inline">
<input type="radio" ng-model="QualificationDetails.qualification_type" value="prof" name="radio1" id="radio5">
<label for="radio5">professional</label>
</div>
</div>
</div>


//This is the drop down that I need to diable

<div class="form-group">
<label class="col-sm-2 control-label" for="Qulitype">Qualification type</label>
<div class="col-sm-10">

<select name="repeatSelect" id="repeatSelect" ng-disabled="QualificationDetails.qualification_type == 'prof'" ng-model="QualificationDetails.qualification" class="form-control">
<option ng-repeat="quali in qualiLevel" value="{{quali.qualification_id}}">{{quali.quali_level}}</option>
</select>
</div>
</div>


This is the code I implemented to work above scenario. But didn't work :(

regController.js

$scope.$watch('QualificationDetails.qualicication_type', function (QualiType) {
if (angular.isUndefined($scope.QualificationDetails)) {
return;
}
if (QualiType === 'prof') {
$scope.QualificationDetails.qualification_type = $scope.QualiType;
}
else {
if ($scope.QualificationDetails.qualification_type !== null) {
$scope.QualiType = $scope.QualificationDetails.qualification_type;
$scope.QualificationDetails.qualification_type = null;
}
}
});


the above scenario is that when it comes to qualifications if qualification type is equal to professional (prof) drop down list is disabled and when it is educational the drop down list should be enabled. Any idea on how to achieve this.

Answer

It seems to me, that your code works fine without watcher you have added. I hope I understood what you want correctly. Try this snippet:

angular
    .module('app', [])
    .controller('Ctrl', function($scope) {
        $scope.qualiLevel = [
          {quali_level: 'A', qualification_id: 1},
          {quali_level: 'B', qualification_id: 2}
        ];
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>

<div ng-app="app">
<div ng-controller="Ctrl">
  
<div class="form-group">
    <label class="col-lg-2 col-md-3 control-label">Qualification</label>
        <div class="col-lg-10 col-md-9">
            <div class="radio-custom radio-inline">
                <input type="radio" ng-model="QualificationDetails.qualification_type" value="edu" name="radio1" id="radio4">
                    <label for="radio4">Educational</label>
            </div>
            <div class="radio-custom radio-inline">
                <input type="radio" ng-model="QualificationDetails.qualification_type" value="prof" name="radio1" id="radio5">
                    <label for="radio5">professional</label>
            </div>
        </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label" for="Qulitype">Qualification type</label>
        <div class="col-sm-10">

            <select name="repeatSelect" id="repeatSelect" ng-disabled="QualificationDetails.qualification_type == 'prof'" ng-model="QualificationDetails.qualification" class="form-control">
                <option ng-repeat="quali in qualiLevel" value="{{quali.qualification_id}}">{{quali.quali_level}}</option>
            </select>
        </div>
</div>
</div>
</div>

Comments