0
我有这个控制器。每当我尝试将我的$ scope.things绑定到从服务器接收到的JSON时,我的视图就不会更新。事实上,如果我离开我的push(newThing)
并取消我的电话getAllThings()
,我的视图将显示数据,并在不到一秒钟的时间内将其删除。为什么这种双向数据绑定不起作用?
问题是:在服务器端(Morgan使用Node和MongoDB运行),我打印出json对象,然后将其传递给我的客户端,并且该对象是正确的,它具有最新创建的对象。
我愿意使用推送而不是向服务器发送另一个请求来获取数据,但是我对这个话题感到非常不满。另一个有趣的事情是,我的第二次,第三次和因此致电insertThing()
将正确更新我的观点。它让人觉得有点无知。如果需要,我可以提供更多的代码。
控制器:
$scope.insertThing= function(newThing){
$scope.things.push(newThing);
$http.post('http://localhost:8080/things', newThing)
.then(function(res){
//$scope.getAllThings();
delete $scope.newThing;
}, function(res){
});
//$scope.getAllThings();
};
$scope.getAllThings = function(userDpt){
$http.post('http://localhost:1234/things/dpt/', {userDpt: userDpt})
.then(function(res){
$scope.things = res.data;
}, function(res){
});
};
节点服务器与猫鼬:
app.post('/things/dpt', function(req, res){
var department = req.body.dpt;
var things= {};
TicketModel.find({ fromDpt: department},
function(err,obj){
res.json(obj);
});
});
标记:
<tr ng-repeat="thing in things">
<td> {{ thing.toDpt}} </td>
<td> {{ thing.status }}</td>
<td> {{ thing.deadline }}</td>
<td><button class="btn close" data-ng-click="deleteThing(thing._id)">×</button></td>
</tr>
http://jsfiddle.net/arunpjohny/wknqxqmq/1/? –
在你的'getAllThings'函数中,data.json来自哪里?我没有看到它在任何地方定义。 – raduation
这听起来像是你加载数据的顺序问题,然后调用你的insertThing方法。用您提供的代码很难说清楚。我们可以做更多的事情。 –