AngularJS checkbox dynamic ng-true-value expression
angularjs, checkbox, javascript, json
Solution
It seems `ng-true-value` does not accept non-constant expressions. From the docs(v1.3.0):
Some attributes used in conjunction with ngModel (such as ngTrueValue or ngFalseValue) will only accept constant expressions.
Examples using constant expressions include:
<input type="checkbox" ng-model="..." ng-true-value="'truthyValue'">
<input type="checkbox" ng-model="..." ng-false-value="0">
Examples of non-constant expressions include:
<input type="checkbox" ng-model="..." ng-true-value="someValue">
<input type="checkbox" ng-model="..." ng-false-value="{foo: someScopeValue}">
An ideal workaround probably would be calling a Controller method on `ng-click` or `ng-change` inside which you can analyse all the checkboxes for truthy or non-truthy values.
Problem
I'm trying to build a calculator for daycare prices in Angular. Every location in the company franchise has separate prices for every day. So my thinking was to build a form, with first a select that allows you to select the location, then a series of checkboxes for the days. I'm having trouble with ng-true-value in the checkboxes selecting the correct prices from my json file. UPDATE: Added Plunkr: http://plnkr.co/edit/MDmrqaH1VzLBzjd5eHgT?p=preview Consider this code: ``` <p class="kind_section">Choose location</p> <select ng-model="formData.location" ng-options="location.title for location in data.bso"></select> <p class="kind_section">Select days</p> <input type="checkbox" ng-model="location.day.mon" ng-change="calculatePrice()" ng-true-value="{{data.bso[formData.location.ID].prices.monday}}" ng-false-value="0">Ma <input type="checkbox" ng-model="location.day.tue" ng-change="calculatePrice()" ng-true-value="{{data.bso[formData.location.ID].prices.tuesday}}" ng-false-value="0">Di<br /> <input type="checkbox" ng-model="location.day.wed" ng-change="calculatePrice()" ng-true-value="{{data.bso[formData.location.ID].prices.wednesday}}" ng-false-value="0">Wo <input type="checkbox" ng-model="location.day.thu" ng-change="calculatePrice()" ng-true-value="{{data.bso[formData.location.ID].prices.thursday}}" ng-false-value="0">Do<br /> <input type="checkbox" ng-model="location.day.fri" ng-change="calculatePrice()" ng-true-value="{{data.bso[formData.location.ID].prices.friday}}" ng-false-value="0">Vr ``` First the select sets formData with a location ID, then I want to use this ID to select the day prices for the matching location and set those to ng-true-value. I'm using `ng-true-value="{{data.bso[formData.location.ID].prices.monday}}"` for this. This doesn't work. When I set the ID manually like `ng-true-value="{{data.bso[0].prices.monday}}"` it does work. Why is the result of the select not being picked up by ng-true-value? This is my json file: ``` $scope.data = { "bso": [ { "ID": 0, "title": "Locatie 1", "prices": { "monday": 130, "tuesday": 130, "wednesday": 200, "thursday":130, "friday": 130 } }, { "ID": 1, "title": "Locatie 2", "prices": { "monday": 430, "tuesday": 530, "wednesday": 600, "thursday":990, "friday": 730 } } ] }; ```