AngularJS checkbox dynamic ng-true-value expression

前端 未结 3 621
不知归路
不知归路 2020-12-17 10:19

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 bui

相关标签:
3条回答
  • 2020-12-17 10:56

    Expression in the ng-true-value will be evaluated only once, so it won't be dynamic.

    One alternative approach is to calculate the values in ng-change callback instead.

    Please see my fork http://plnkr.co/edit/9zYS3OZ0sSkXX9rHwcgv?p=preview for the full example.

    In html:

    <input type="checkbox" ng-model="selectedDays.monday" ng-change="calculatePrice()" /> Mon
    <input type="checkbox" ng-model="selectedDays.tuesday" ng-change="calculatePrice()" /> Tue            <br />
    <input type="checkbox" ng-model="selectedDays.wednesday" ng-change="calculatePrice()" /> Wed
    <input type="checkbox" ng-model="selectedDays.thursday" ng-change="calculatePrice()" /> Thu            <br />
    <input type="checkbox" ng-model="selectedDays.friday" ng-change="calculatePrice()" /> Fri
    

    and in controller:

    $scope.calculatePrice = function(){
      $scope.formData.location.day = {};
    
      angular.forEach($scope.selectedDays, function (selected, day) {
        if (selected) {
          $scope.formData.location.day[day.slice(0, 3)] = $scope.data.bso[$scope.formData.location.ID].prices[day];
        }
      });
    }
    
    $scope.selectedDays = {};
    
    0 讨论(0)
  • 2020-12-17 11:04

    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.

    0 讨论(0)
  • 2020-12-17 11:08

    Another approach is to delay the creation of the checkbox until the value is ready (on scope or whatever).

    In my case I was loading a value via http that wasn't on the scope when the checkbox was created. So I just wrapped it in an ng-if.

              <div class="checkbox" ng-if="viewData.conditionId != undefined">
                <label>
                  <input type="checkbox" ng-true-value="{{'\''+ viewData.conditionId + '\''}}" ng-false-value="undefined" ng-model="model.conditionId" required />
                  I agree
                </label>
              </div>
    

    Which worked perfectly for my scenario. Yours is a bit different but the same principal should apply - delay creation of the checkbox until you know the value being bound is there.

    And yes the stupid quotation marks seem to be necessary.

    0 讨论(0)
提交回复
热议问题