0

I know I'm doing something wrong, but here is the code:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<form>
    <input type="submit" ng-disabled="false" value="Not Disabled"/> <!-- Shouldn't be disabled -->
    <br>
    <input type="submit" ng-disabled="true" value="Disabled"/> <!-- Should be disabled -->
</form>

Why isn't the second submit button disabled? I searched online and I thought you could use ng-disabled to have a boolean as the disabled attribute.
Thanks.

MBJH
  • 1,571
  • 3
  • 17
  • 36

2 Answers2

1

The button will only be disabled if the expression evaluates to true and the parent element has the ng-app attribute.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<form>
    <input type="submit" ng-disabled="false" value="Not Disabled"/> <!-- Shouldn't be disabled -->
    <br>
    <div ng-app ng-init="disableBtn=true">
<input type="submit" ng-disabled="disableBtn" value="Disabled"/> <!-- Should be disabled -->
</div>
    
</form>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

The code needs an ng-app directive to boot AngularJS. Otherwise the ng-disabled directive will not be compiled:

̶<̶f̶o̶r̶m̶>̶
<form ng-app>

The DEMO

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<form ng-app>
    <input type="submit" ng-disabled="false" value="Not Disabled"/> <!-- Shouldn't be disabled -->
    <br>
    <input type="submit" ng-disabled="true" value="Disabled"/> <!-- Should be disabled -->
</form>
georgeawg
  • 48,608
  • 13
  • 72
  • 95