我有一个角度应用程序,它有一个带数字的文本输入框。此框使用packsToReplenish
绑定到ng-change
。它还有updateAllCalculations()
,可以调用updateAllCalculations
。我的想法是,当我在文本字段中输入数字时,模型会更新,然后执行<input type="number" class="form-control" ng-model="packsToReplenish" ng-change="updateAllCalculations()" required>
。问题是,据我所知,模型永远不会更新,而是保持在0,无论我进入框中的是什么。
因此,我的console.log始终为绑定模型的输出显示0,然后由于数据不准确而导致逻辑失败。有谁能告诉我如何解决这个问题?
以下是相关的代码段:
HTML
$scope.packsToReplenish = 0;
$scope.samples = []; //populated elsewhere
$scope.updateAllCalculations = function () {
for (var i = 0; i < $scope.samples.length; i++)
$scope.updateCalculations(i);
}
$scope.updateCalculations = function(idx) {
console.log("Update Calculations " + idx + " - " + $scope.packsToReplenish); //always displays 0, and because of this the logic below doesnt execute properly
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;
}
}
}
JavaScript(当然,在应用程序和控制器中 - 省略了以节省空间)
switch/case
答案 0 :(得分:0)
添加例如超时100毫秒。帮助我。
$scope.updateAllCalculations = function(idx) {
$timeout(function() {
...
}, 100)
}