samedi 28 février 2015

Can Angular do two way data binding backwards between two input fields?


I can't make my two input fields update values when changing the opposite input.


What I want to do is make a basic $dollar to Gold oz calculator that takes in two input fields. Sample Preview: http://ift.tt/1DEmBoU


The first input is the dollar amount, the second an ounces amount. There is a third variable that contains the gold selling rate.


In my code I have been able to successfully changing the Dollar amount and make the oz amount update via angular two way data binding.


The problem is trying to make the opposite work; for example, changing the oz amount should also update the dollar amount.


Here is my html code:



<div class="row panel" ng-controller="ctrl">

<!-- Dollar input -->
<label class="small-6 columns">Dollar $:
<input type="number" ng-model="dollar">
</label>

<!-- Ounces input -->
<label class="small-6 columns">Ounces (oz):
<input type="number" value="{{ ozCalc() | number:4 }}">
</label>

</div>


Here is my angular code:



var app = angular.module('app', []);

app.controller('ctrl', ['$scope', function($scope) {
$scope.dollar = 0;
$scope.oz = 0;
$scope.rate = 1267;

//Dollar to Ounce Calculator
$scope.ozCalc = function() {
var oz = $scope.dollar / $scope.rate;
return oz;
};

//Ounce to Dollar Calculator
$scope.dollarCalc = function() {
var dollar = $scope.oz * $scope.rate;
return dollar;
};

}]);


Any help would be really appreciated. Thanks





Aucun commentaire:

Enregistrer un commentaire