国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

angular.js - [angular]陣列清空後,視圖沒(méi)有馬上更新
PHP中文網(wǎng)
PHP中文網(wǎng) 2017-05-15 16:52:44
0
3
736

最近在開(kāi)發(fā)一個(gè)資料清單時(shí)遇到問(wèn)題:
此資料清單有一個(gè)搜尋功能,輸入時(shí),會(huì)立即根據(jù)輸入的關(guān)鍵字,從服務(wù)端請(qǐng)求搜尋結(jié)果。然後視圖馬上渲染結(jié)果。具體程式碼如下:

app.controller('ListViewController',function($scope){

    $scope.files=[];

    $scope.vm={
        key:''
    };

    $scope.query=function(){
        var param={
            nameFuzzy:$scope.vm.key
        }
        $http.post(url,param)
            .success(function(resp){
                angular.each(resp,function(item){
                    $scope.files.push(item);
                });
            });
    };

    $scope.$watch('vm.key',function(newVal,oldVal){
        if(newVal!==oldVal){
            //關(guān)鍵詞發(fā)生變化時(shí),清空列表
            $scope.files.length=0;
            //然后請(qǐng)求數(shù)據(jù)
            $scope.query();
        }
    });

    $scope.query();
});

現(xiàn)在的問(wèn)題在於:當(dāng)清空數(shù)組時(shí),視圖上的列表沒(méi)有消失,待搜索結(jié)果返回後,並渲染成功,前一個(gè)列表才消失,也就是說(shuō),兩組數(shù)據(jù)會(huì)同時(shí)存在幾百毫秒的樣子,前一組資料才消失,呼叫$scope.$apply()並沒(méi)有什麼用,會(huì)拋出錯(cuò)誤:degist in progress,說(shuō)明已經(jīng)在更新視圖中,但是不知道為什麼會(huì)這麼慢。
ps:還有其它資料列表,則沒(méi)有這個(gè)問(wèn)題

PHP中文網(wǎng)
PHP中文網(wǎng)

認(rèn)證高級(jí)PHP講師

全部回覆(3)
滿(mǎn)天的星座

試試調(diào)用scope.$digest();這個(gè)有沒(méi)有用?

Peter_Zhu

app.controller('ListViewController',function($scope){

$scope.files=[];

$scope.vm={
    key:''
};

$scope.query=function(){
    var param={
        nameFuzzy:$scope.vm.key
    }

    $scope.files=[]; //增加

    $http.post(url,param)
        .success(function(resp){
            angular.each(resp,function(item){
                $scope.files.push(item);
            });
        });
};

});

模版中 關(guān)鍵字輸入框 使用ng-change="query()" 就可以了。 不要濫用watch 除非你明白怎麼用

洪濤


`$timeout(function(){
$scope.files = [];
})`

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板