重新渲染整个ng-repeat

时间:2014-04-18 18:16:48

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

我无法重新渲染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;
    };
})

2 个答案:

答案 0 :(得分:0)

您不会在$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

答案 1 :(得分:0)

您需要观察currentPaginationValue并更新数字数组。 这是你的傻瓜的分叉:http://plnkr.co/edit/jrMTiKV34v01GELdwlEz?p=preview

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

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