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方法。用您提供的代碼很難說清楚。我們可以做更多的事情。 –