2016-11-15 67 views
1

这是从服务器加载列表的功能。最初显示列表,但是当应用过滤器时获得空响应时,它仍显示先前的结果并且不清除先前的列表。为什么ng-repeate对象没有得到更新?

$scope.browseListing = function (strURL) { 
     $scope.CurrentTab = strURL; 
     $scope.getURL(strURL); 
     $http.post($scope.URL) 
       .then(function (response) { 
        if (response.data != 'null') { 
         $scope.Data = response.data; 
         $scope.TotalListingCount = $scope.Data.length;            
         $window.alert('Result is not null'); 
        } 
        else { 
         $scope.TotalListingCount = '0'; 
         $window.alert('Result is null'); 
         $scope.Data = []; 
        } 
       }, function (response) { 
        $log.info(response); 
       }); 
    }; 

编辑

enter image description here

如何解决这个问题,这样就空响应前面的清单将被清除,并没有显示出列表?

+0

更新从服务器 – Aravind

+0

@Aravind检索您的各自的HTML标记和JSON数据,我需要显示/隐藏NG-repeate按反应? – JMD

+1

当没有数据时,您是否收到“Result is null”警报? –

回答

1

可能是您的范围没有更新。请在下面试试这个(这不是100%的好办法,但就在这个时候你就可以解决您的问题)

if(!$scope.$$phase) { 
    $scope.$apply(
     $scope.Data = []; 
     ); 
    }  
    $scope.TotalListingCount = '0'; 
    $window.alert('Result is null'); 
  • 并请有任何错误控制台。

更新: 尝试这样一种方式(全局声明空对象)

.then(function (response) { 
          $scope.TotalListingCount = '0'; 
          $scope.Data = []; 
         if (response.data != 'null') { 
          $scope.Data = response.data; 
         $scope.TotalListingCount = $scope.Data.length;            
          $window.alert('Result is not null'); 
         } 
         else { 
           $window.alert('Result is null'); 
           } 
        } 

It's does not works well, then please share your filter code. Bcs the problem should be there.

+0

控制台错误,错误:$ rootScope:inprog 已执行操作# – JMD

+0

删除缓存。 – stormec56

+0

@ stormec56,清除缓存仍然无法正常工作... – JMD

0

下创建新的范围,并prototypically继承:ng-repeatng-includeng-switchng-viewng-controller,directivescope: true,directivetransclude: true

因此,使用$parent'ng-repeate' to reference to parent scope instead of using newly created scope by NG-repeat`原样

<tr ng-repeat="listing in $parent.Data | orderBy : sortColumn : SortDirection">

UIng-repeat加入$parent到scope属性后,它更新UI按变化。

Here is full description of scope