我正在 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>
我还在 slider 后添加了两个单选按钮:
<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>
如何根据单选选择更改 slider 的最小/最大值?我尝试使用 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;
}
最佳答案
使用 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>
您的 Controller :
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;
}
};
});
关于javascript - 根据 Angularjs 中的单选选择更改变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396665/