Javascript 当通过http请求动态插入值时,Angularjs ng repeat不更新
我正在使用http获取一些数据,并更新Javascript 当通过http请求动态插入值时,Angularjs ng repeat不更新,javascript,angularjs,Javascript,Angularjs,我正在使用http获取一些数据,并更新$scope.comments变量。然后我使用ng repeat显示它,但是变量正在正确更新,但ng repeat不起作用。下面是javascript: $scope.updateComments = function(taskId){ var contentInput = document.getElementById(taskId); contentInput.value = ''; $http.get('/loadTask/'+
$scope.comments
变量。然后我使用ng repeat显示它,但是变量正在正确更新,但ng repeat不起作用。下面是javascript:
$scope.updateComments = function(taskId){
var contentInput = document.getElementById(taskId);
contentInput.value = '';
$http.get('/loadTask/'+taskId).success(function(task){
angular.forEach(task.comments,function(comment){
$http.get('/loadComment/'+comment).success(function(c){
$scope.comments[comment] = c;
console.log('Comments now',$scope.comments);
});
});
});
}
我无法使用$scope.$apply
,因为$http
请求已经在调用它。
这是html:
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
{{comments}}
<div class="comments" ng-repeat="comment in task.comments track by $index">
<p><a href="{{comments[comment].author}}">
{{comments[comment].author}}
</a>{{comments[comment].content}}</p>
</div>
</div>
</div>
{{comments}}
{{comments[comment].content}
另外,{comments}
正在正确更新。
谁能帮帮我,提前谢谢 在ng repeat中,您循环了不是属性的task.comments$scope@PrinceG但是我正在接受属于scopeyeah的注释,但是task.comments只能在成功回调中访问。基本上你是在你的ng repeat中循环一个未定义的变量->
ng repeat=“comment in task.comments track by$index”
@PrinceG噢,非常感谢你!顺便说一句,你真的想对每条评论发出一个新的$http请求吗?