gpt4 book ai didi

javascript - AngularJS:模型不在 ng-change 上更新?

转载 作者:行者123 更新时间:2023-11-30 06:52:47 24 4
gpt4 key购买 nike

我有一个 Angular 应用程序,它有一个带数字的文本输入框。此框使用 ng-model 绑定(bind)到 packsToReplenish。它还有一个调用 updateAllCalculations()ng-change。我的想法是,当我在文本字段中输入数字时,模型会更新,然后执行 updateAllCalculations。问题是,据我所知,无论我在框中输入什么,模型都不会更新,而是保持在 0。

正因为如此,我的console.log对于绑定(bind)模型的输出总是显示0,然后由于数据不准确导致逻辑失败。谁能告诉我如何解决这个问题?

以下是相关的代码片段:

HTML

<input type="number" class="form-control" ng-model="packsToReplenish" ng-change="updateAllCalculations()" required>

JavaScript(当然在应用程序和 Controller 中 - 只是为了节省空间而省略了)

$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;
}
}
}

最佳答案

添加超时,例如100 毫秒。对我有帮助。

$scope.updateAllCalculations = function(idx) {
$timeout(function() {
...
}, 100)
}

关于javascript - AngularJS:模型不在 ng-change 上更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32195375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com