karthiks karthiks - 1 year ago 158
AngularJS Question

AngularJS ng-disabled directive with expression is not working

User Story: When a new user clicks the New User checkbox and accepts the TermsAndConditions radio button, the Register button should be enabled.

My code in angularJS isn't working. The Register button remains disabled. Wondering what went wrong?

<!DOCTYPE html>
<html ng-app>
<script src="lib/angular/angular.min.js"></script>

<div ng-controller="LoginCtrl">
<label>New User?</label>
<input type="checkbox" ng-model="isNewUser"/>

<div ng-show="isNewUser">
<label>Accept Terms and Conditions</label>
<input type="radio" value="yes" name="tnc" ng-model="tnc"/><label>Yes</label>
<input type="radio" value="no" name="tnc" ng-model="tnc" /><label>No</label>

<input type="submit" value="Login" ng-disabled="isNewUser" >
<input type="submit" value="Register" ng-show="isNewUser" ng-disabled="hasAcceptedTnC('{{tnc}}')">

<script language="JavaScript">
var LoginCtrl = function ($scope) {
$scope.isNewUser = false;
$scope.tnc = "no";

$scope.hasAcceptedTnC = function(value) {
return "yes"==value;

Answer Source

The ng-disabled expression is evaluated in the present scope. Hence, you should only need the following without any extra interpolation with {{..}}:

    <input type="submit" 

Note that I added a ! since you probably want the button disabled if the user has not accepted the TnC.

Working demo: http://plnkr.co/edit/95UiO4Rd2IMh8T1KjSQK?p=preview

A comment was posted below asking how to reason about when to use {{...}} and when to use bare expression with a given ng-* directive. Unfortunately, there is no syntactical clue hidden in the directives which can reveal that information. Looking at the documentation will turn out to be fastest way to find out this information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download