gpt4 book ai didi

javascript - 动态更改 Angular $scope 变量

转载 作者:行者123 更新时间:2023-11-28 00:12:02 25 4
gpt4 key购买 nike

我有两个数组(作用域变量),其值会动态更新,但在 HTML 中,当我尝试打印它们的值时, View 不会采用更新后的值。

<div>
<!-- array printing -->
<span ng-repeat="var in array track by $index">{{var}}</span>
</div>
<div>
<!-- array -->
<span ng-repeat="var in array2 track by $index">{{var}}</span>
</div>

当我在控制台日志中打印值时,数组正在更改,但其更改并未反射(reflect)在页面中。

controller.js

$scope.update = function() {

$scope.array.push(box);

// inside an ajax call
var index = $scope.array.indexOf(box);

if(index > -1) {
$scope.array.splice(index,1);
console.log("index "+index);
}
}

最佳答案

看来您的 Ajax 调用没有被 AngularJS 调用。也许您正在使用 jQuery?

在这种情况下,请尝试在 Ajax 回调函数结束后使用 $scope.$apply();。它将验证任何更改并更新您的 View 变量。

您可以阅读有关 $apply here 的更多信息和 here .

关于javascript - 动态更改 Angular $scope 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30797742/

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