Javascript 当中间添加行时,检测NG重复完成的时间

Javascript 当中间添加行时,检测NG重复完成的时间,javascript,angularjs,Javascript,Angularjs,发布的每个涉及检测ng repeat何时完成的解决方案都依赖于范围。$last值,如下所示: angular.module('fooApp') .directive('onLastRepeat', function () { return function (scope, element, attrs) { if (scope.$last) { setTimeout(function () {

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

angular.module('fooApp')

    .directive('onLastRepeat', function () {
        return function (scope, element, attrs) {
            if (scope.$last) {
                setTimeout(function () {
                    scope.$emit('onRepeatLast', element, attrs);
                }, 1);
            }
        };
    })
问题是,此解决方案仅在附加到列表末尾时有效。如果在中间插入数据,Simult.$XOST将始终为false,并且您不知道ANGARORJS何时完成对新绑定行的更新。无论ng repeat在何处渲染数据,是否有任何解决方案都有效

例如:

html:


如果在上述代码为$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);
        }
    };
})

“如果你在中间插入数据”是什么意思?你能给出一个更具体的例子来说明你的意思吗?当然可以,检查它是的,但是“中间”触发器将触发多次而不是一次(假设你添加了多个元素),所以它在语义上不完全相同。@Marius我无法重现这种行为(多重触发)。检查此代码笔:
$scope.addToList = function() {
    $scope.list.splice(1, 0, 4);
}

$scope.list = [1, 2, 3];
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);
        }
    };
})