" /> " /> "/>

AngularJS - Submit button not disabling (im a newbie)

44 Views Asked by At

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.

2

There are 2 best solutions below

2
Unmitigated On BEST ANSWER

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>

0
georgeawg On

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>