AngularJS how to get and edit calculated input ng-model value related to other input value -
i trying make currency exchange system spa based on angularjs.
there should several inputs currency data json (with ng-repeat) , convert currency rate depending on active input value.
{ "currencies": [ { "name":"eur", "rate": 1, "active": "no" },{ "name": "pln", "rate": 4.1028, "active": "no" }, { "name": "usd", "rate": 1.1239, "active": "no" } ] }
<div ng-repeat='cur in main.currencies'> <label>{{cur.name}}</label> <input type="number" min="0" ng-model="cur.result" ng-init='cur.result = ""' ng-click='main.clear();' ng-focus='main.active = cur; cur.active = true'; ng-blur='cur.active = false'; > </div>
how input 1 value on active (focused) input , calculated values on other inputs. new angularjs , want learn practical exercises.
other values should follow such logic: cur.value = main.active.value / main.active.rate * cur.rate
.
but if try instead of ng-model="cur.result"
:
ng-model="cur.result = main.active ? cur.result : cur.result/main.active.rate*cur.rate"
i can't write in input
maybe miss ng*...
in input properties, far know if use ng-model
should not use ng-name
, ng-value
...
try js:
angular.module('app',[]); angular.module('app').controller('ratectrl', ratectrl); function ratectrl($scope){ var vm = $scope; vm.cur = {}; vm.currencies = {/* object */} vm.setrate = function(model, rate){ return // javascript } }
the html
<div ng-controller="ratectrl main"> <div ng-repeat="currency in main.currencies"> <label>{{currency.name}}</label> <!-- fyi, in ng-repeat there isolated scope. '$parent.' must come before objects outside ng-repeat --> <input ng-model="main.cur.result" ng-focus="main.setrate(main.cur.result, currency.rate)"> </div> </div>
this 1 basic way manipulate ng-model object. alternatives include using angular's filters, watchers, or decorators.
Comments
Post a Comment