【发布时间】:2015-08-10 12:49:29
【问题描述】:
我正在尝试通过从数据库中获取数据来更新数据列表。每次我会添加一个数据,它不会更新视图,只有当我刷新页面时视图才会更新。我通过使用 $scope.apply 并重新排列我的代码的位置尝试了一些解决方案,这些没有任何区别。我在这里想念什么?以下是我的代码:
JS
//posting post
$scope.post = function(){
$http.post('/post',$scope.post_detail)
.success(function(response){
$scope.render();
});
}
//getting post
$scope.render = function(){
$http.get('/post')
.success(function(response){
$scope.renderPost(response);
});
}
//view post
$scope.renderPost = function(response){
$scope.posts = response;
}
$scope.remove_post = function(id){
$http.delete('/post/'+id).success(function(response){
$scope.render();
});
}
$scope.render();
HTML
<div class="jumbotron text-center" ng-controller="DashboardCtrl">
<h1>{{title}}</h1>
<input type="text" ng-model="post_detail.title" />
<input type="text" ng-model="post_detail.border_color" />
<button ng-click="post(post_detail)">Post</button>
</div>
<div ng-repeat="post in posts">
<p>{{post.title}}</p>
<button ng-click="remove_post(post._id)">Remove</button>
</div>
注意:删除按钮在这里有效
【问题讨论】:
-
你检查响应结构了吗?
-
@RIYAJKHAN,是的,我做到了。它确实返回了正确的结构。但仍然没有运气
标签: javascript angularjs angularjs-scope