我正在使用 Angular JS 构建一个社交网络应用程序。我对绑定(bind)数据有一个疑问。有一个时间线 div,其中显示所有最近的帖子。此外,顶部还有一个状态更新器。现在,在页面加载时,我向数据库触发 ajax($http) 服务并查询所有帖子,获取 json 数据,然后使用 ng-bind 将其绑定(bind)到 div。假设现在更新状态,如何在不刷新页面的情况下让更新的数据显示在我的 View 中?我对此感到困惑。请帮帮我。
谢谢, 帕万
编辑## - 一些好人要求提供代码,这里是..我仍然面临问题并且无法绑定(bind)我的json数据:(请帮忙。
wall.controller('mainData', function($scope, $http)
{
$scope.status = {};
$scope.updatedStatus = {};
$scope.getStatus = function(){
var uid = localStorage.getItem('_id');
$http({
method: "GET",
url: "http://localhost:8000/profile/status/"+uid
}).success(function (data, status, headers, config) {
$scope.updatedStatus =data;
})
}
和html部分
<div class="card" ng-controller="mainData" ng-repeat="item in updatedStatus">
<div class="card-header">
<div class="media">
<div class="pull-left">
<img class="lv-img" src="img/profile-pics/2.jpg" alt="">
</div>
<div class="media-body m-t-5">
<h2><a href="profile.html" class="a-title">{{item.firstname+" "+item.lastname}}</a><small>{{item.record_time}}</small></h2>
</div>
</div>
</div>
<hr/>
<div class="card-body card-padding">
<p>{{item.message}} </p>
</div>
</div>
我从服务器获取的 JSON 数组
[
{firstname: "Pavan"
id: 1
lastname: "Kumar"
message: "This is my first update"
record_time: "10/16/2015, 8:32:16 PM"},
{firstname: "LOL"
id: 2
lastname: "test"
message: "This is my second update"
record_time: "10/16/2015, 8:32:16 PM"}
]
正如我所说,在我向服务器添加新值后,没有任何内容显示,也没有与当前数据一起显示数据。但服务器返回了我 json 对象,没有显示错误。 ng-repeat 可能有问题吗? .任何帮助都将受到高度赞赏。 :)
最佳答案
您的数据存在 3 个位置
1- 数据库
2- JavaScript 对象
3- html View
因此,如果您进行了绑定(bind),则 javascript 对象中的值将与 html View 中的值相同
因此,如果您更改了 javascript 对象中的值,它也会在 View 中更改 在你的问题中你说的是
i fire an ajax($http) service to the DB
我不知道你是否使用 Angular $http 服务或其他服务,但如果你使用 Angular 函数,就不会有问题,但如果你使用非 Angular 函数来获取 http 请求,你需要在请求回调函数结束
$scope.$apply();
要求摘要循环工作并在 View 上应用您的更改
关于javascript - Angular JS 从数据库更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32933465/