检测指令

时间:2017-05-16 18:56:18

标签: javascript jquery angularjs angularjs-ng-repeat

我有angular-sly-carousel指令和一些数据。当用户向下滚动时我的数据正在添加,因此在滚动之后我需要重新加载狡猾的轮播选项。

如何检测ng-repeat元素长度何时发生变化?

我的来源,我应该检测ng-repeat元素的长度变化(不起作用)。

<li sly-horizontal-repeat-simple ng-repeat="game in value | limitTo: gamesLimit track by game.id"> 
... 
</li>


VBET5.directive('slyHorizontalRepeatSimple', function($timeout,$window) {
return {
    restrict: 'A',
    link: function(scope, el, attrs) {
        if (scope.$last === true) {
            $timeout(function() {
                var cont = $(el[0]);
                var frame = $(el[0]).parent();

                // Call Sly on frame
                $(window).on("resize", function() {
                    frame.sly("reload");
                });

                // Watch , if items in ng-repeat change length then reload
                scope.$watch('value', function() {
                    frame.sly("reload"); // I need reload sly when el chang
                });


            });
        }
    }
}
});

1 个答案:

答案 0 :(得分:1)

您必须使用scope变量而不是element.length。如果Scope.variable得到修改,则会触发监视触发器。

$scope.$watch(
            function () {
                return $scope.value;
            },
            function (newValue, oldValue) {
                if (!angular.equals(oldValue, newValue)) {
                  $scope.text = 'Your value now =' + $scope.value;
                    }
            },
            true);