簡體   English   中英

為什么這兩種方式的數據綁定不起作用?

[英]Why doesn't this two way data binding work?

我有這個控制器。 每當我嘗試將$ scope.things綁定到從服務器收到的JSON時,我的視圖都不會更新。 實際上,如果我離開了push(newThing)並取消了對getAllThings()調用的注釋,則我的視圖將顯示數據,並在不到一秒鍾的時間內將其刪除。

問題是:在服務器端(運行Node和MongoDB的摩根),在將json對象傳遞給客戶端之前,我正在打印該對象,並且該對象是正確的,它確實具有最新創建的對象。

我願意使用push而不是向服務器發出另一個請求以獲取數據,但是我對此話題真的很沮喪。 另一個有趣的事情是,我對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)">&times;</button></td>
</tr> 

好吧,這是很久以前的事了。

所有這些代碼都已完全重構。 問題出在JavaScript的異步行為。 有時,getAllThings()函數會在刪除行之前完成,而新獲取的數據將在收到后消失。

解決此問題的方法是在http請求之后添加另一個回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM