【问题标题】:Re-rendering entire ng-repeat重新渲染整个 ng-repeat
【发布时间】:2014-06-03 07:08:21
【问题描述】:

我无法重新渲染 ng-repeat。 ng-repeat 连接到一个范围变量,我正在尝试使用 ng-click 进行更改。 ng-click 时变量的值会发生变化,但 ng-repeat 不会重新渲染。我已经尝试过所有关于使用 $scope.$apply 的建议,但我无法实现它。请帮忙。

在此处查看 plunker:http://plnkr.co/edit/iuh6tMrrjkKxCvQABaa5?p=preview

相关代码如下: (一个 ng-click 可以改变 $scope.currentPaginationValue,这在 Batarang 中很明显,但它不会导致重新渲染与 $scope.numbers 关联的 ng-repeat)

.controller('numbersController', function($scope, dataService) {
    $scope.currentPaginationValue = 4;
    $scope.numbers = dataService.fetchNumbers($scope.currentPaginationValue);
    $scope.gotoPrevPage = function() {
            $scope.currentPaginationValue = $scope.currentPaginationValue - 1;
    };
})

【问题讨论】:

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


    【解决方案1】:

    您需要查看 currentPaginationValue 并更新 numbers 数组。 这是你的 plunkr 的一个分支:http://plnkr.co/edit/jrMTiKV34v01GELdwlEz?p=preview

    你在数字控制器中需要这样的东西:

    $scope.$watch('currentPaginationValue', function(newVal) {
         $scope.numbers = dataService.fetchNumbers($scope.currentPaginationValue);
    });
    

    【讨论】:

      【解决方案2】:

      您没有在$scope.numbers 中分配新值。

      试试这个:

      .controller('numbersController', function($scope, dataService) {
          $scope.currentPaginationValue = 4;
          $scope.numbers = dataService.fetchNumbers($scope.currentPaginationValue);
          $scope.gotoPrevPage = function() {
              $scope.currentPaginationValue = $scope.currentPaginationValue - 1;
              $scope.numbers = dataService.fetchNumbers($scope.currentPaginationValue); // add this line
          };
      })
      

      Live demo

      【讨论】:

      • 我一直在努力阅读有关 $digest、$apply 的所有内容……谢谢!
      猜你喜欢
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2015-11-22
      相关资源
      最近更新 更多