javascript - AngularJS: Model Not Updating On ng-change? -
i have angular app has text input box takes number. box bound using ng-model
packstoreplenish
. has ng-change
calls updateallcalculations()
. idea being when enter number in text field, model updates, , updateallcalculations
executes. problem is, far can tell model never updates, instead staying @ 0, regardless of enter box.
because of this, console.log shows 0 output of bound model, , logic fails due inaccurate data. can tell me how fix this?
here pertinent code snippets:
html
<input type="number" class="form-control" ng-model="packstoreplenish" ng-change="updateallcalculations()" required>
javascript (in app , controller of course - omitted save space)
$scope.packstoreplenish = 0; $scope.samples = []; //populated elsewhere $scope.updateallcalculations = function () { (var = 0; < $scope.samples.length; i++) $scope.updatecalculations(i); } $scope.updatecalculations = function(idx) { console.log("update calculations " + idx + " - " + $scope.packstoreplenish); //always displays 0, , because of logic below doesnt execute if ($scope.samples[idx].packages[0].openbox > 0) { if ($scope.samples[idx].packages[0].openbox >= $scope.packstoreplenish) { $scope.samples[idx].packages[0].maxunitstouse = $scope.packstoreplenish; $scope.samples[idx].packages[0].newopenboxunits = $scope.samples[idx].packages[0].openbox - $scope.packstoreplenish; } else { $scope.samples[idx].packages[0].maxunitstouse = $scope.samples[idx].packages[0].openbox; $scope.samples[idx].packages[0].newopenboxunits = 0; } } }
add timeout of e.g. 100 milliseconds. helped me.
$scope.updateallcalculations = function(idx) { $timeout(function() { ... }, 100) }
Comments
Post a Comment