【问题标题】:Why ng-repeate object not getting updated?为什么 ng-repeat 对象没有得到更新?
【发布时间】:2016-11-16 15:40:03
【问题描述】:

这是从服务器加载列表的功能。最初会显示列表,但当应用过滤器得到空响应时,它仍然显示以前的结果并且不清除以前的列表。

 $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);
                });
    };

已编辑

我该如何解决这个问题,以便在空响应时清除以前的列表并且不显示列表?

【问题讨论】:

  • 更新从服务器检索到的相应 html 标记和 json 数据
  • @Aravind,我需要根据响应显示/隐藏 ng-repeate 吗?
  • 当没有数据时,您是否收到“结果为空”警报?
  • 是的,当返回空响应时弹出警报..
  • @jmd 并不完全取决于您正在寻找的解决方案。如果有一些 HTML,我们至少可以尝试找出问题所在。

标签: angularjs angularjs-scope angularjs-ng-repeat


【解决方案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');
                              }
                    }

效果不好,请分享您的过滤器代码。 Bcs问题应该在那里。

【讨论】:

  • 控制台错误,Error: $rootScope:inprog Action Already In Progress
  • 删除缓存。
  • @Ramesh Rajendran,我也试过!$scope.$$phase。添加$$phase后控制台没有错误但问题仍然存在。
  • 当你得到空数据时,你能告诉我你的响应 JSON 吗?
  • @RameshRajendran,我有更新问题,响应为空。
【解决方案2】:

以下创建新的范围,并继承原型:ng-repeatng-includeng-switchng-viewng-controllerdirectivescope: truedirectivetransclude: true。 /p>

所以,使用$parent'ng-repeate' to reference to parent scope instead of using newly created scope byng-repeat` as-

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

ng-repeat 中的$parent 添加到UI 中的范围属性后,它会根据更改更新UI

Here is full description of scope

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多