angularjs:当行添加到在中间时,检测ng-erepeat何时结束

angularjs: detect when ng-repeat finishes when rows added in the middle

本文关键字:检测 ng-erepeat 何时 结束 在中间 添加 angularjs      更新时间:2023-09-26

发布的每个涉及检测ng重复何时完成的解决方案都依赖于范围$最后一个值,如下所示:

angular.module('fooApp')
    .directive('onLastRepeat', function () {
        return function (scope, element, attrs) {
            if (scope.$last) {
                setTimeout(function () {
                    scope.$emit('onRepeatLast', element, attrs);
                }, 1);
            }
        };
    })

问题是,只有当您将其追加到列表的末尾时,此解决方案才有效。如果在中间插入数据,请确定范围$最后一个总是错误的,你不知道angularjs什么时候完成了对新绑定行的更新。有没有人有一个解决方案,无论ng repeat在哪里渲染数据都能工作?

示例:

html:

<button ng-click="addToList()"></button>
<table>
    <tbody>
        <tr ng-repeat="foo in list" on-last-repeat><td><span>{{foo}}</span></td></tr>
    </tbody>
</table>

控制器代码:

$scope.addToList = function() {
    $scope.list.splice(1, 0, 4);
}
$scope.list = [1, 2, 3];

如果您在上面代码为$scope.last的示例中单击按钮,则该指令将在渲染完成时触发。

你能用这样的东西吗?

angular.module('fooApp')
.directive('onLastRepeat', function () {
    return function (scope, element, attrs) {
        if (scope.$middle) {
            setTimeout(function () {
                scope.$emit('onRepeatMiddle', element, attrs);
            }, 1);
        }

        if (scope.$last) {
            setTimeout(function () {
                scope.$emit('onRepeatLast', element, attrs);
            }, 1);
        }
    };
})