3

How to enable or disable a button according to a select value? I want a button to be disabled but once a select value is changed to a certain vaue "xxxx" the button is enabled.

1
  • 3
    Very simple: ngModel + ngDisabled directives. Check documentation. Commented Apr 25, 2016 at 9:14

5 Answers 5

5

Use the ng-disabled directive like this:

<div class="form-group">
    <label class="control-label"> Etat</label>
    <select class="form-control" name="singleSelect"  ng-model="demande.etat">
        <option value="etude">etude</option>
        <option value="Accepte">Accepte</option>
        <option value="Refus">Refus</option>
    </select><br>
</div>

and in the button markup use something like this:

<button ng-disabled="demande.etat != 'Accepte'">
    ....
</button>
Sign up to request clarification or add additional context in comments.

10 Comments

my angularjs doesn't recognize the ng-disabled.
@SaidiMeriem Which version of angular are you using? Btw. does the app not recognize the directive or does it not bind to your data? Please inlcude your controller & html sources in your original post so that we can see where the problem might be.
I'm using AngularJS v1.4.7 and yes the app is not recognizing the directive
I've edited my hint to reflect what you have in your document. 1.4.7 should work with ng-disabled. So the question is, is it managed by the same app/controller? You can try to simply write true and false in the ng-disabled directive to check if the app is actually recognizing the directive there.
the ng-disabled is working, but not the propriate way, it is disabled even when demande.etat = 'Accepte'
|
1

Use ngDisabled for more details visit: https://docs.angularjs.org/api/ng/directive/ngDisabled

Use the model value of the select box to enable or disable

OR

Post you code so that can be improvised

Comments

0
<button class="btn btn-success btn-sm" ng-click="genererContrat(d)" title="Contrat"  data-toggle="modal" data-target="#myModalHorizontalContrat" ng-disabled="{demande.etat == Accepte}" ><span class="glyphicon glyphicon-eye-open" ></span><span class="hidden-xs hidden-sm" ></span></button>

Comments

0

check this... if you want chage then comment..

angular.module("myApp",[]).
controller("myController",function($scope){
$scope.buttonShow=false;
  $scope.checkVal=function(){
    if($scope.demande.etat == "Accepte"){
      $scope.buttonShow=true;
    }else{
      $scope.buttonShow=false;    
    }
  }
});
<html ng-app="myApp" >
  <head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 </head>
  <body ng-controller="myController">
<div class="form-group" >
 <label class="control-label"> Etat</label>
<select ng-change="checkVal()" class="form-control" name="singleSelect"  ng-model="demande.etat">
<option value="etude">etude</option>
<option value="Accepte">Accepte</option>
<option value="Refus">Refus</option>
</select><br>
  
  <button ng-show="buttonShow" class="btn btn-success btn-sm" ng-click="genererContrat(d)" title="Contrat"  data-toggle="modal" data-target="#myModalHorizontalContrat"  ><span class="glyphicon glyphicon-eye-open" ></span><span class="hidden-xs hidden-sm" ></span>Button</button>
</div>
    </body>
  </html>

Comments

-1

You can use ng-if also for conditions

$scope.MyButton = true;

<button ng-if="MyButton === false">
  ....
</button>

3 Comments

This will stop the button from being rendered at all.
@anierzad if you use ng-if at proper place it will show proper data for proper condition
But it doesn't answer the question asked by the original poster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.