gpt4 book ai didi

javascript - AngularJS 在 $scope 数组上使用 ng-repeat

转载 作者:行者123 更新时间:2023-11-30 12:32:51 25 4
gpt4 key购买 nike

我有一个 Controller ,它通过 Parse 成功检索对象并将其推送到数组中:

var mySite = angular.module('mySite', []);

mySite.controller('listConnectors', ['$scope',
function ($scope) {
//Parse.initialize here;
$scope.array = [];
var TestObject = Parse.Object.extend("TestObject");
var query = new Parse.Query(TestObject);
query.find({
success: function (results) {
alert("Successfully retrieved " + results.length + " rows.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
alert(object.id + ' - ' + object.get('foo') + " " + object.get('num'));

/***** Pushing onto array here *******/
$scope.array.push(object);

}

console.log($scope.array[0].attributes.foo); //Grabbed what was needed

},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
}]);

然后,我尝试循环并列出数组中每个对象的“foo”值,但没有成功,如上面的 console.log 所示。然而,没有任何输出。好像 ng-repeat 可能没有被执行或输入:

    <li ng-repeat="eachElement in array">
<a > {{eachElement.attributes.foo}}</a>
</li>

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

我能够修复它。感谢 Jonathan Lonowski 的评论和链接。

我不得不用 $apply 将推送包装到数组中:

$scope.$apply(function(){
$scope.array.push(object);
});

关于javascript - AngularJS 在 $scope 数组上使用 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27051017/

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