我是AngularJs的新手,并且遇到ng-repeat问题。基本上,我想使用ajax从数据库中获取与文章相关的评论,然后使用ng-repeat显示它们。然后,我有了一个数组,可以在其中推送我的评论。我的问题是,当我在数组中手动创建注释时,它可以很好地工作,但是如果我从ajax函数的回调中自动推送此注释,则会更新数组,而不是视图。
以html格式查看:
var articleApp = angular.module('articleApp', []);
articleApp.controller('CommentsController', function CommentsController($scope) {
$scope.comments = [];
// This push works well, my view is updated
$scope.comments.push({
content: "Hello world !",
date: "2 minutes ago",
id: 29,
author: {
pseudo: "Sean"
}
});
// When I push with this function, my array is updated, but not the view
$scope.addComment = function(comment) {
$scope.comments.push({
content: comment.comment,
id: comment.id,
date: comment.date_post,
author: {
id: comment.author.id,
pseudo: comment.author.pseudo
}
});
};
var articleID = document.getElementById('articleID').textContent;
// getComments is defined elsewhere, and returns the 20 first comments
getComments(20, articleID, 0, function(comments) {
for(var i = 0; i < comments.length; i++) {
$scope.addComment(comments[i]);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section id="commentsSection" class="bottom_apps" ng-controller="CommentsController as comments">
<article id = "{{comment.id}}" class="comment_container" ng-repeat="comment in comments">
<div class="comment_header">
<span class="comment_author">{{comment.author.pseudo}}</span>
<span class="comment_date">{{comment.date}}</span>
</div>
<div class="comment_content">
{{comment.content}}
</div>
</article>
</section>
我仔细检查了所有代码,三遍检查了所有代码,但看不到我在哪里出错了。
答案 0 :(得分:1)
由于您传递的回调函数以注释为参数,因此看起来getComments
异步工作。
因此,即使您在该回调中更新了注释,AngularJS似乎也不会“注意到”,对吗?
这是因为您必须明确告知AngularJS运行新的摘要周期。
简而言之,只需在回调的末尾添加$scope.$apply()
:
getComments(20, articleID, 0, function(comments) {
for(var i = 0; i < comments.length; i++) {
$scope.addComment(comments[i]);
}
$scope.$apply();
});
要了解更多信息,请搜索“ AngularJS摘要循环”。简而言之,就是:AngularJS在所谓的摘要周期中更新所有内容。如果没有发生摘要循环,AngularJS将不会“通知”更改。当事物同步运行时,AngularJS自动运行摘要循环。但是对于许多异步的事情,AngularJS无法自动解决,因此您必须明确地告诉AngularJS执行摘要循环。
答案 1 :(得分:0)
您可以尝试执行以下代码,也可以使用一些虚拟操作检查this plunker link,以查看给定的示例方案。
控制器:
$scope.comments = [];
$scope.comment={};
// This push works well, my view is updated
$scope.comments.push({
content: "Hello world !",
date: "2 minutes ago",
id: 29,
author: {
pseudo: "Sean"
}
});
$scope.comments.push({
content: "Hello world 2!",
date: "5 minutes ago",
id: 30,
author: {
pseudo: "Jack"
}
});
// When I push with this function, my array is updated, but not the view
$scope.addComment = function() {
$scope.comments.push({
content: $scope.comment.comment,
id: $scope.comments.length+1,
date: new Date(),
author: {
id: $scope.comments.length+1,
pseudo: $scope.comment.comment
}
});
console.log($scope.comments);
};
模板:
<section id="commentsSection" class="bottom_apps" ng-controller="CommentsController">
<input type="text" ng-model="comment.comment"/>
<button type="button" ng-click="addComment()">Add Comment</button><br/>
<article id = "{{comment.id}}" class="comment_container" ng-repeat="comment in comments">
<div class="comment_header">
<span class="comment_author">{{comment.author.pseudo}}</span>
<span class="comment_date">{{comment.date}}</span>
</div>
<div class="comment_content">
{{comment.content}}
</div>
</article>
</section>