我需要根据用户选择的货币显示同一商品的不同价格(已经从后端提供了不同的价格,因此无需进行任何进一步的转换)。我想用ng-click
更改表达式我的HTML是这样的:
<div ng-controller="myCtrl">
you have, {{exchange.[currency]}}...
<br>
<br>
<select>
<option ngclick="currency='USD'">USD</option>
<option ngclick="currency='Eur'">Eur</option>
</select>
</div>
我的控制员:
var app = angular.module('myApp', []);
// controller here
app.controller('myCtrl', function($scope) {
$scope.currency = "USD";
$scope.exchange = [{
"USD": 199,
"Eur": 20
}];
})
您可以在JSfiddle here
中查看上面的代码答案 0 :(得分:1)
这就是你如何以Angular的方式做到这一点。
HTML:
<div ng-controller="myCtrl">
you have, {{currency.rate}} {{currency.code}}
<br>
<br>
<select ng-options="currency.code for currency in exchange" ng-model="currency"></select>
</div>
控制器
var app = angular.module('myApp', []);
// controller here
app.controller('myCtrl', function($scope) {
$scope.exchange = [{
"code": "USD",
"rate": 199
},
{
"code": "EUR",
"rate": 20
}];
$scope.currency = $scope.exchange[0];
})