我想将我从公式获得的值转换为cm / mm / inch,只需从ng-options中选择,它就会自动更新。任何想法我怎么能只使用ng-options中的选择而不使用任何按钮?
这是代码示例
variable.html
<h1>{{value}}</h1>
<select ng-model = "length.selectedUnit" ng-options="x.unit for x in velogas1.availableUnits track by x.unit">
app.js
$scope.length = {
availableUnits: [
{unit: 'mm'},
{unit: 'cm'},
{unit: 'Inch'},
{unit: 'm'}
],
selectedUnit: {unit: 'm'}
};
答案 0 :(得分:2)
您可以在选择中使用ng-change
来启动计算功能。
编辑:如果您更改选项以包含乘数,则可以让计算在插值器中工作,而无需ng-change
。
angular.module('test', []).controller('Test', Test);
function Test($scope) {
$scope.value = 3.18; // in meter
$scope.units = [
{name: 'm', multiplier: 1},
{name: 'inch', multiplier: 39.3701},
{name: 'cm', multiplier: 10},
{name: 'mm', multiplier: 100}
]
$scope.selectedUnit = $scope.units[0];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.1/angular.min.js"></script>
<div ng-app='test' ng-controller='Test'>
<div>{{ value * selectedUnit.multiplier }} {{selectedUnit.name}}</div>
<select ng-model="selectedUnit" ng-options="unit as unit.name for unit in units"></select>
</div>