当信息发生变化时,有没有办法将信息从模型传递到视图,例如消息传递应用中的新帖子。
我的控制器:
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);
});
}
}
我的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>
任何有用的东西,Gab
答案 0 :(得分:0)
尝试在$ apply()函数中包装要更新的代码块。