angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Feature: ngTrueValue should accept non-string values

Open coli opened this issue 14 years ago • 60 comments

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.

coli avatar Jul 25 '12 01:07 coli

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?

btford avatar Aug 01 '12 08:08 btford

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.

coli avatar Aug 01 '12 16:08 coli

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'}">
</>

coli avatar Aug 01 '12 16:08 coli

Heck, I have a far simpler use case that I was disappointed to discover doesn't work - I need an integer, rather than string!

jamiedgordon avatar Sep 05 '12 18:09 jamiedgordon

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.

scyros avatar Sep 09 '12 09:09 scyros

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.

adamsch1 avatar Nov 12 '12 22:11 adamsch1

+1. Support expression in ng-true-value

arselvam avatar Nov 28 '12 11:11 arselvam

+1 Support integer in ng-true-value

redbarmaley avatar Jan 11 '13 16:01 redbarmaley

+1 for expression instead of string

mogadget avatar Feb 20 '13 21:02 mogadget

+1 Support expressions!

ilmotta avatar Feb 27 '13 15:02 ilmotta

+1 for expressions

brab avatar Mar 04 '13 23:03 brab

+1 Support objects:

<div ng-repeat="group in groups">
    <input type='checkbox' ng-model="array[$index]" ng-true-value="group" />
</div>

marcospassos avatar Mar 06 '13 03:03 marcospassos

@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 avatar Mar 06 '13 18:03 brab

@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)

coli avatar Mar 07 '13 00:03 coli

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.

lkende avatar Mar 28 '13 22:03 lkende

+1

daspecster avatar Apr 16 '13 18:04 daspecster

+1 for expressions

jan-j avatar Apr 21 '13 13:04 jan-j

+1. Support expression in ng-true-value

NiltonCruz avatar May 03 '13 15:05 NiltonCruz

+1 on supporting expressions! In my case, need to build a form dynamically. Eg: http://jsfiddle.net/federosky/N8Wjm/5/

federosky avatar May 06 '13 21:05 federosky

+1 for integer

rbuc avatar May 08 '13 19:05 rbuc