gpt4 book ai didi

javascript - angularJS $scope 更改不更新 View

转载 作者:行者123 更新时间:2023-11-29 14:43:09 24 4
gpt4 key购买 nike

我一直在努力解决 angularJS $scope 问题。我有一个在 Controller 函数中更新的数组。根据我的控制台日志,其中的值确实发生了变化。但是, View 不会更新。这是片段。

//array initialisation

$scope.fieldSuggestions = []

//modifying function

$scope.changeCurrentInput = function(fieldName, eye) {
for(var field in $scope.test){
console.log($scope.test[field].name)
if($scope.test[field].name === fieldName){
console.log($scope.test[field].values)
console.log("be4 update")
console.log($scope.fieldSuggestions)
$scope.fieldSuggestions = $scope.test[field].values;
console.log("after update")
console.log($scope.fieldSuggestions)
}
}
};

//view

<div ng-repeat="choice in fieldSuggestions">
<button class="button button-positive">{{choice}}</button>
</div>

更多详情...

我认为这可能是相关的。我有一个父 View 和几个 subview 。这些路由中的每一个都使用相同的 Controller 。即:routes.js 中的属性 Controller 对所有人都是相同的。调用修改函数的按钮在 subview 中,但未更新的 ng-repeat 在父 View 中。

最佳答案

由于您正在更改嵌套属性,因此 Angular 不会触发摘要。您可以通过添加 linkcontroller 函数来简单地解决这个问题:

$scope.$watchCollection('fieldSuggestions', function(newFieldSuggestions, oldFieldSuggestions) {
$scope.fieldSuggestions= newFieldSuggestions;
});

需要指出的事情:对于大型集合和复杂对象,这可能不是最佳选择。

关于javascript - angularJS $scope 更改不更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35977203/

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