Feature: ngTrueValue should accept non-string values
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.
I'm not entirely sure of the purpose of ngTrueValue, except that it seems like a convenience directive for adding watches to a controller for one case. I suspect that if you want to set some model to a complex value, it might be cleaner to put a $scope.$watch on your checkbox's model and modify a model on the scope accordingly.
@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)
<ng-repeat="group in groups>
<input type='checkbox' ng-model="selectedArray" ng-true-value="group" mi-checkbox="{keyField:'id'}">
</>
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! In my case, need to build a form dynamically. Eg: http://jsfiddle.net/federosky/N8Wjm/5/
+1 for integer