How do I add multiple conditions to "ng-disabled"?

All we need is an easy explanation of the problem, so here it is.

I need to check that two conditions are both true before enabling a button:

Here is an example:

<button type="submit" ng-disabled="frmUser.pw2.$error.pwMatch" class="btn btn-primary" ng-click="ChangePassword()">Change</button>

This example only contains one condition within ng-disabled. How would I add another such as a scope variable?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You should be able to && the conditions:

ng-disabled="condition1 && condition2"

Method 2

Wanny is correct. The && operator doesn’t work in HTML. With Angular, you must use the double pipes (||)for multiple conditions.

Method 3

There is maybe a bit of a gotcha in the phrasing of the original question:

I need to check that two conditions are both true before enabling a button

The first thing to remember that the ng-disabled directive is evaluating a condition under which the button should be, well, disabled, but the original question is referring to the conditions under which it should en enabled. It will be enabled under any circumstances where the ng-disabled expression is not “truthy”.

So, the first consideration is how to rephrase the logic of the question to be closer to the logical requirements of ng-disabled. The logical inverse of checking that two conditions are true in order to enable a button is that if either condition is false then the button should be disabled.

Thus, in the case of the original question, the pseudo-expression for ng-disabled is “disable the button if condition1 is false or condition2 is false”. Translating into the Javascript-like code snippet required by Angular (https://docs.angularjs.org/guide/expression), we get:

!condition1 || !condition2

Zoomlar has it right!

Method 4

Make sure you wrap the condition in the correct precedence

ng-disabled="((!product.img) || (!product.name))"

Method 5

Actually the ng-disabled directive works with the ” || ” logical operator for me. The ” && ” evaluate only one condition.

http://jsbin.com/kuqilejesi/2/edit?html,js,output

Method 6

You can try something like this.

<button class="button" ng-disabled="(!data.var1 && !data.var2) ? false : true">
</button>

Its working fine for me.

Method 7

Both variable must be false for the button to be disabled:

<button class="button" ng-disabled="(!data.var1 && !data.var2) ? false : true">
</button>

Method 8

this way worked for me

ng-disabled=”(user.Role.ID != 1) && (user.Role.ID != 2)”

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply