Javascript 变量更改时在AngularJS中更新视图
当信息发生更改(例如消息传递应用程序中的新帖子)时,是否有方法将信息从模型传递到视图 我的控制器:Javascript 变量更改时在AngularJS中更新视图,javascript,angularjs,Javascript,Angularjs,当信息发生更改(例如消息传递应用程序中的新帖子)时,是否有方法将信息从模型传递到视图 我的控制器: function commentsController($scope, $http){ $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; $http.get("index.php?action=getComments")
function commentsController($scope, $http){
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http.get("index.php?action=getComments")
.success(function(data){ $scope.comments = data; });
$scope.addComment = function(comment){
if("undefined" != comment.msg){
$http({
method : "POST",
url : "index.php",
data : "action=add&msg="+comment.msg
}).success(function(data){
$scope.comments.unshift(data);
});
$scope.comment = "";
}
}
$scope.deleteComment = function(index){
$http({
method : "GET",
url : "index.php?action=delete&id="+$scope.comments[index].id,
}).success(function(data){
$scope.comments.splice(index,1);
});
}
}
My index.php:
<div>
<textarea name="submitComment" ng-model="comment.msg" placeholder="What are you thinking?"></textarea>
<a href="javascript:void(0);" class="button" ng-click="addComment(comment)">POST</a>
</div>
<!-- Comments -->
<div ng-repeat="comment in comments">
<div class="updates">
<a href="javascript:void(0);" ng-click="deleteComment($index);" style='float:right'>Delete</a>
{{comment.msg}}
</div>
</div>
</div>
{{comment.msg}}
如果有任何帮助,Gab尝试将要更新的代码块包装到$apply()函数中。如何使用该服务每隔xx秒更新一次
$scope.comments
?