AngularJS | ng-disabled Directive

The ng-disabled Directive in AngularJS is used to enable or disable HTML elements. If the expression inside the ng-disabled attribute returns true then the form field will be disabled or vice versa. It is usually applied on form field (input, select, button, etc).

Syntax:

<element ng-disabled="expression"> Contents... </element>

Example 1: This example uses ng-disabled Directive to disable the button.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>ng-disabled Directive</title>
  
    <script src=
    </script>
</head>
  
<body ng-app="app" style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
      
    <h2>ng-disabled Directive</h2>
      
    <div ng-controller="app" ng-init="disable=false">
        <button ng-click="geek(disable)" ng-disabled="disable">
            Click to Disable
        </button>
          
        <button ng-click="geek(disable)" ng-show="disable">
            Click to Enable
        </button>
    </div>
      
    <script>
        var app = angular.module("app", []);
        app.controller('app', ['$scope', function ($app) {
            $app.geek = function (disable) {
                $app.disable = !disable;
            }
        }]);
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
ngdisabled
After clicking the button:
ngdisabled

Example 2: This example uses ng-disabled Directive to enable and disable button using checkbox.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>ng-disabled Directive</title>
  
    <script src=
    </script>
</head>
  
<body ng-app="app" style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
      
    <h2>ng-disabled Directive</h2>
      
    <div ng-controller="geek">
        <h4>
            Check it 
            <input type="checkbox" ng-model="check" />
        </h4>
          
        <button type="button" ng-disabled="!(check)">
            Click to submit
        </button>
    </div>
      
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope', function ($scope) {
            $scope.disableClick = function (isDisabled) {
            $scope.isDisabled = !isDisabled;
            }
        }]);
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
ngdisabled
After clicking the button:
ngdisabled



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.