0

我正在 angularjs 中构建一个计算器作为 wordpress php 插件。通过简码将参数作为变量传递。我有一个 jquery ui-slider,其最小值、最大值和步长值由传递的参数设置:

<div ui-slider="{range: 'min'}" min="{$this->weight_minimum}"
     max="{$this->weight_maximum}" step="{$this->weight_increment}"
     ng-model="weight">
</div>

我还在滑块后添加了两个单选按钮:

<div class="user-selection">
    <label class="radio-container">{{ "Pounds" | translate }}
      <input type="radio" checked="checked" name="radio"
             ng-model="selection" value="lbs">
      <span class="checkmark"></span>
    </label>
    <label class="radio-container">{{ "Kilograms" | translate }}
      <input type="radio" name="radio" ng-model="selection" value="kg">
      <span class="checkmark"></span>
</label>
</div>

如何根据单选选择更改滑块的最小/最大值?我尝试使用 ng-if 但那是为了显示/隐藏 html。我还尝试在 app.controller 中添加一个 if 语句,但没有成功。

if ($scope.selection == 'lbs') {
  weight_maximum = $this->weight_maximum;
} else if ($scope.selection == 'kg') {
  weight_maximum = $this->weight_maximum / 2.2;
}
4

1 回答 1

1

在plunker代码中使用 ng-change 事件示例 :html

  <div class="user-selection">
    <label class="radio-container"> Pounds
      <input type="radio" checked="checked" name="radio" ng-change="changUnit()" ng-model="selection" value="lbs">
      <span class="checkmark"></span>
    </label>
    <label class="radio-container"> Kilograms
      <input type="radio" name="radio" ng-model="selection" value="kg" ng-change="changUnit()">
      <span class="checkmark"></span>
    </label>
  </div>

  <div>Min : {{weight_minimum}}</div>
  <div>Max : {{weight_maximum}}</div>
  <div>slider value : {{weight}}</div>
  <br/>
  <br/>


  <div ui-slider min="{{weight_minimum}}" max="{{weight_maximum}}" step="{{weight_increment}}" ng-model="weight">
  </div>

你的控制器:


    var app = angular.module('plunker', ['ui.slider']);

    app.controller('MainCtrl', function($scope) {

      $scope.selection = 'kg'
      $scope.weight_minimum = 10;
      $scope.weight_maximum = 1000;
      $scope.weight_increment = 1;
      $scope.weight = 50;

      $scope.changUnit = function() {
        if ($scope.selection == 'lbs') {
         $scope.weight_maximum = $scope.weight_maximum * 2.2;
        } else if ($scope.selection == 'kg') {
          $scope.weight_maximum =  $scope.weight_maximum / 2.2;
        }

      };
    });

于 2019-03-28T18:00:11.370 回答