2

I was stuck with using negate boolean values,

Refer the below code

<label>Click me to toggle: <input type="checkbox" ng-model="!checked"></label><br/>

<button ng-model="button" ng-disabled="!checked">Button</button>

If i use above [ngModel:nonassign] error throws

how to bind negote boolean expression for ng-model value?

Thanks in advance

3
  • Possible duplicate of How to disable an input box using angular.js Commented Dec 19, 2016 at 6:54
  • Can you add a code snippet? Commented Dec 19, 2016 at 7:15
  • Remove the !: ng-model="checked". Commented Dec 19, 2016 at 7:17

2 Answers 2

4

With AngularJS 1.3+ you can use ng-true-value and ng-false-value. This will reverse the value.

var app = angular
  .module('app', [])
  .controller('ctrl', function($scope) {
    $scope.checked = true;
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <label>
    Click me to toggle:
    <input type="checkbox" ng-model="checked" ng-true-value="false" ng-false-value="true">
  </label>
  <button ng-model="button" ng-disabled="!checked">Button</button>
</div>

Sign up to request clarification or add additional context in comments.

Comments

-1

Try using ng-disabled tag rather than enabled. You cannot set ng-model="!checked" since the model variable must be a variable, which cannot include expressions. You can adjust the condition inside the ng-disabled tag for that. Please check the code snippet.

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.bootcss.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

    <div ng-app="myApp" ng-controller="myCtrl">
        <label>Click me to toggle:
            <input type="checkbox" ng-model="checked">
        </label>
        <br/>

        <button ng-model="button" ng-disabled="checked">Button</button>
    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {

        });
    </script>

</body>

</html>

You can find the documentation of ng-disabled here and example here

5 Comments

input type button accepts negoted value but check box can not accept
Why do you need negotiated values?
if i check or uncheck ,Need to reverse the operation of other elements behaviour
@MaheshP ng-disabled uses unidirectional binding: the expression is read, only, and is used to add or remove the disabled attribute. ng-model uses bidirectional binding: the value of the checkbox is written to the model. You can do $scope.checked = newValue but $scope.!checked = newValue doesn't make any sense.
On checking or unchecking the checkbox, the model value will update automatically, no need to reverse the model value. Please check my code snippet. It automatically updates the disabled state as the checkbox is checked or unchecked.

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.