-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Feature: ngTrueValue should accept non-string values #1199
Comments
I'm not entirely sure of the purpose of @coli, do you have a specific use case in mind? @IgorMinar, @mhevery, @vojtajina, what do you guys think? |
Use case is fairly common actually: I have a array of say category objects in say $scope.categories=[{id:5,name:"First"},...} Then I have a selectedCategories array that contains a subset of those categories. I want to show a checkbox for some categories(paging), and when you check or uncheck the checkbox, selectedCategories is changed. I actually wrote a directive that will overwrite the default checkbox behavior to add support for the above. I can share the code if needed. |
My usage ended up like this, keyField was needed to do object comparison as in my case, selected group object and group object could be slightly different initially (they came from 2 different backend API)
|
Heck, I have a far simpler use case that I was disappointed to discover doesn't work - I need an integer, rather than string! |
I'm using a template to render a checkbox control. In this case i cannot know the true value, that value depends on specific control information i want to render. |
I too would prefer that ng-true-value be an expression. If your number of checkboxes is determined at run time you have to resort to scope methods with timeout wrappers. |
+1. Support expression in ng-true-value |
+1 Support integer in ng-true-value |
+1 for expression instead of string |
+1 Support expressions! |
+1 for expressions |
+1 Support objects: <div ng-repeat="group in groups">
<input type='checkbox' ng-model="array[$index]" ng-true-value="group" />
</div> |
@coli would you mind sharing the directive you wrote? It would be a great help to me while this is issue is still being considered. Maybe in a gist? |
@brab I wrote it at a previous job unfortunately, but basically, it's similar to the built in code for checkboxes, I copy/pasted into a custom directive and added expression support. (+ a ugly hack to remove the the built-in one) |
Expressions would be great! However, even javascript truth testing for integers would be expected. I use $resource to populate an object that returns integer values (1 or 0) from a database boolean. These are then attached to the $scope for which the ng-model on the input is bound. Was really surprised to find out setting ng-true-value="1" would not recognize integer 1 as true and check the box. Will look into other methods mentioned in the meantime. |
+1 |
+1 for expressions |
+1. Support expression in ng-true-value |
+1 on supporting expressions! |
+1 for integer |
1+ for integer |
+1 for integer (or any JS number) |
👍 for supporting expressions please! please! pleaseeee! :-) |
+1 at least for integer support, preferably expressions as well. |
How about someone puts forward a PR for supporting numbers? |
Suggested approach for supporting numerical true/false values in checkbox inputs.
documentation update to convey support for both string and number values in ngTrueValue and ngFalseValue attributes
+1 |
+1 for integer type |
+1 |
+1 for expressions |
+1 |
+1 |
2 similar comments
+1 |
+1 |
👍 These are the hacks that are required now (many WTFs):
|
+1 |
+1 it's been two years of "+1"s. Why aren't we supporting this yet? |
@jancarloviray, +1 |
ngTrueValue and ngFalseValue now support parsed expressions which the parser determines to be constant values. BREAKING CHANGE: Previously, these attributes would always be treated as strings. However, they are now parsed as expressions, and will throw if an expression is non-constant. To convert non-constant strings into constant expressions, simply wrap them in an extra pair of quotes, like so: <input type="checkbox" ng-model="..." ng-true-value="'truthyValue'"> Closes angular#8041 Closes angular#5346 Closes angular#1199
Too bad: To me this means things got more complicated. Now I tried So this means I have to replace a simple implementation (ngTrueValue) with a more complex implementation (watch or Object.defineProperty), so actually the opposite of what was intended. Why this artificial restriction?
|
@eekboom - you can still use your interpolation: http://plnkr.co/edit/LQ6OpFjkjiFLxxMJ9hxj?p=preview |
@petebacondarwin - he can use the interpolation, but it won't help him: http://plnkr.co/edit/nNwjEkPIDJwe60HWX950?p=preview |
@dliebscher - my understanding was that @eekboom only wanted to initialise it once:
|
Yes and no: I want to initialize it only once, but only after the proxy configuration has been loaded via xhr. So it changes (from undefined) once after the view is initialized. |
@eekboom
to
Which works, but looks dumb |
I just had an interesting realization. What I was trying to do - and from the looks of it, what a lot of you have been trying to do - can actually be accomplished with:
This supports string values, booleans and numbers. Not sure yet whether I actually like this, but I certainly found it entertaining. And maybe this will help somebody else. |
getu-lar i've tried what you said, with a complex object, it works. In the ng-false-value i filled with |
getu-lar 's solution worked for me. Thanks. |
I was trying to use an expression which resolved to a string with underscore in it, e.g. "101_3". I used getu-lar 's solution and it simply works. Feels like a hack though. Thanks for sharing it ! |
+1 for expressions |
+1 |
It doesn't seem like there is much hope for Angular team to provide a sane checkbox directive for 1.x series... It can't be too difficult. Expression support would be fully backwards compatible due to the mandatory quoting. Until then the suggested workaround is working for me: ng-true-value="{{ myVariable | json }}" Ensure that |
For ng-model checkboxes, ngTrueValue only supports string. It should really just accept an angular expression and eval to get the value so it supports say a js object.
The text was updated successfully, but these errors were encountered: