将所有 <input> 值设置为相同的值乘以其他值

Setting all <input> values to the same thing multiplied by something else

我正在编写一个转换货币的小应用程序。此应用程序包含多个输入字段,它们应该全部相等,并乘以特定比率。到目前为止,我一直在使用以下内容,有效

index.html

<input type="number" ng-model="currencies.value"></input> //input field
<div>{{currencies.value * currency.rate}}</div> //output fields, many of these

app.js

app.controller('CurrencyController',function($scope){
    this.currencies = currencies;
    this.value = 1.00;
});

然而,这不是我想要做的。问题是,只有一个输入字段被使用,而输出字段是 div。我想要完成的是,所有的输入和输出字段都是这样的(速率由循环预定义):

<input type="number" ng-model="currencies.value * currency.rate"></input> //input field
<input type="number" ng-model="currencies.value * currency.rate"></input> //output field, many of these

不幸的是,我不知道如何使用 ng-model 来乘以输入字段的值,因为它只设置一个值,而不是一个函数。

我是不是漏掉了什么?提前致谢!

使用 ng-change(添加父项 'currency' 以进行良好实践的建模')

<input type="number" ng-model="currency.input" ng-change="update('output', 'input', currency.rate)"/>
<input type="number" ng-model="currency.output" ng-change="update('input', 'output', 1/currency.rate)"/>

你的控制器应该看起来像

//Initialize the current object
$scope.currency = {
    input: 0,
    output: 0,
    rate: 1.5
}

$scope.update = function(newVal, val, rate){
    $scope.currency[newVal] = $scope.currency[val] * rate;
}

http://plnkr.co/edit/P1NGdvEuvfnqOGhx2OxP?p=preview