How do I add multiple conditions to “ng-disabled”?

前端 未结 7 1716
执念已碎
执念已碎 2021-02-05 00:27

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

Here is an example:

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 00:45

    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!

提交回复
热议问题