我是angularjs的新手。我正在嘗試構建簡單的貨幣轉換程序。它有2個下拉菜單,從'貨幣'和'到'貨幣。到目前爲止,貨幣只有一個選擇:INR。我試圖在警告框中顯示轉換結果,對於給定的金額和匯率是硬編碼的。代碼如下:angularjs貨幣轉換器
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
Enter amount: <input type="text" ng-model="amount">
Select 'From' currency:
<select ng-model="myVar">
<option value="usd">USD
<option value="eur">Euro
<option value="gbp">GBP
</select>
Select 'To' currency:
<select ng-model="myVar2">
<option value="inr">INR
</select>
<button ng-click="convert()">Convert</button>
<script>
var app=angular.module('myApp', []);
app.controller('myCtrl', function($scope){
$scope.SwitchFunction = function(myVar){
switch(myVar){
case 'usd': alert($scope.amount + " USD equals" + $scope.amount*70 + "INR");
case 'eur': alert($scope.amount + " EUR equals" + $scope.amount*60 + "INR");
case 'gbp': alert($scope.amount + " GBP equals" + $scope.amount*80 + "INR");
}
};
});
</script>
問:我無法將用戶輸入綁定到下拉列表。 問:我想在ng-click上觸發generate()。我可以和開關一起做嗎?
大多數幫助我瞭解交換機主要聚焦於顯示,而不是計算部分即匯率:(
感謝Patrick,它有所幫助。 – sidd68