-1

I have 2 inputs.

<input type="number" ng-model="samount" ng-change="getSR(samount)">
<input type="number" ng-model="ramount" ng-change="getRS(ramount)">

in controller

$scope.getSR = function(amount) {
      $scope.ramount = parseFloat((amount*5.25).toFixed(3)); 
};

$scope.getRS = function(amount) {
      $scope.samount = parseFloat((amount/5.25).toFixed(3)); 
};

I enter some data in input 1 and do some calculations and update value of input 2 with function in controller. This works, but when I enter data in input 2 reverse calculate and update input 1. For some reason, input 1 value is not updating. I used $scope.$watch to see if input 1 is updated and it is updating in $watch, but not in view. If I reload and start with input 2 it works, input 1 updates, but typing input 1 doesn't update again. they are in same scope but ng-change is different functions.

$scope.$apply() in each function gives error apply already in progress.

Is there way to fix this issue?

EDIT: added example code

feruz
  • 53
  • 10
  • 3
    yeah, it is impossible to help you without your code – bto.rdz Jul 26 '15 at 16:40
  • Without code how can we possibly know what the issue is in the first place? `---input 1---` is useless. Please see : http://stackoverflow.com/help/how-to-ask – charlietfl Jul 26 '15 at 16:46
  • 1
    i will post code, just a moment. – feruz Jul 26 '15 at 16:49
  • maybe i should mention that I am using ionic framework. – feruz Jul 26 '15 at 16:56
  • 3
    Could be a child scope problem since you are breaking the rule of always binding an object to `ng-model`...see http://stackoverflow.com/questions/17606936/angularjs-dot-in-ng-model The video in 2nd answer is very short but worth it – charlietfl Jul 26 '15 at 17:03
  • 1
    @charlietfl thanks a bunch. it was indeed childscope issue, changed scope variables to object and they work as they supposed to. thank you! – feruz Jul 26 '15 at 17:12
  • @charlietfl would you please put it as answer? was very helpful and hope it helps a lot of beginners like myself. – feruz Jul 26 '15 at 17:20
  • 1
    not totally helpful because not enough html shown to see childscopes. I do hope you watched that video... and rule is simple to rememebr `always have a dot in ng-model`. There's another developer benefit also ... a whole form will be in one object...makes it simple to send to server – charlietfl Jul 26 '15 at 17:21

1 Answers1

1

First of all, your code works https://jsfiddle.net/5m18jfy9/ Anyway, I think you miss the rule of dot. When you binding data to your model in scope, use object as namespace. For example:

ng-repeat='carname' //this could brake 2-way data binding in some point
ng-repeat-'car.name' //this will always update properly your model and template 
Eduard Jacko
  • 1,974
  • 1
  • 16
  • 29