1

I do have a directive input-checkbox as a template for <input type="checkbox" .... I am trying to pass values for ng-true-value to this directive and here I am stuck:

I am using the directive like this:

 <input-checkbox ... mv-true-value="false" mv-false-value="true"  mv-disabled="true"></input-checkbox>

and the directive template looks like this:

        <input type="checkbox" ...
           ng-disabled="mvDisabled"
           ng-true-value="mvTrueValue"
           ng-false-value="mvFalseValue">

The value für ng-disabled is correctly compiled as "true" but ng-true-value and ng-false-value are not correctly set.

To narrow down what might be wrong i tried different stuff: This works as aspected:

            <input type="checkbox" ...
           ng-disabled="mvDisabled"
           ng-true-value="false"
           ng-false-value="true">

Also this works as aspected:

            <input type="checkbox" ...
           ng-disabled="mvFalseValue"
           ng-true-value="false"
           ng-false-value="true">

WebStorm tells me ng-true-value is not allowed here but as described above it still works if I set fixed values in the template file. I am working with AngularJS 1.7.2

Any ideas?

EDIT: I forgot this information. The scope in my .js for the directive looks like this:

 scope: {
 ...
  mvDisabled: "=",
  mvTrueValue: "=",
  mvFalseValue: "=",
 ...
}
5
  • Possible duplicate of AngularJS checkbox dynamic ng-true-value expression Commented Oct 15, 2018 at 10:01
  • Short answer: it's not possible. ng-true-value only accepts constant expressions. docs.angularjs.org/error/ngModel/constexpr Commented Oct 15, 2018 at 10:02
  • Also see this for a workaround: stackoverflow.com/questions/17030748/… Commented Oct 15, 2018 at 10:04
  • Thanks for your answer. Not sure if I do the transfer correctly: First of all the value doesn't need to be dynamic. If there is another way to pass a constant to the directive when first rendering that would be then the solution I guess. For the workaround: Since I need to invert true and false, in the controller I would have to manually loop through the data coming from the database, invert every value and doing the same thing again when saving the data back to the database and then one third time after saving to keep the correct output in the frontend. I guess I got the workaround wrong? Commented Oct 15, 2018 at 11:04
  • Since you said you're ok with a constant, you can just pass a literal string. Such as ng-true-value="'potatoes'" Notice the single and double quotes. Commented Oct 15, 2018 at 14:05

0

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.