AngularJS流沙

arg*_*g20 5 javascript jquery quicksand angularjs

有没有办法在Angular中实现jQuery的Quicksand插件?也许有一个实现,但我似乎无法找到它.

也许这样做的策略会对我有所帮助,因为流沙会获取一个列表然后作为参数接收新列表,但是使用Angular重新呈现数据的方式我不知道如何做到这一点.

Gui*_*e86 8

我使用砌体指令实现了类似的东西+ ng-animate用于输入/离开动画,这里是一个仅限CSS动画的演示(以chrome供应商为前缀的CSS):

http://jsfiddle.net/g/3SH7a/

指令:

angular.module('app', [])
.directive("masonry", function () {
    var NGREPEAT_SOURCE_RE = '<!-- ngRepeat: ((.*) in ((.*?)( track by (.*))?)) -->';
    return {
        compile: function(element, attrs) {
            // auto add animation to brick element
            var animation = attrs.ngAnimate || "'masonry'";
            var $brick = element.children();
            $brick.attr("ng-animate", animation);

            // generate item selector (exclude leaving items)
            var type = $brick.prop('tagName');
            var itemSelector = type+":not([class$='-leave-active'])";

            return function (scope, element, attrs) {
                var options = angular.extend({
                    itemSelector: itemSelector
                }, attrs.masonry);

                // try to infer model from ngRepeat
                if (!options.model) { 
                    var ngRepeatMatch = element.html().match(NGREPEAT_SOURCE_RE);
                    if (ngRepeatMatch) {
                        options.model = ngRepeatMatch[4];
                    }
                }

                // initial animation
                element.addClass('masonry');

                // Wait inside directives to render
                setTimeout(function () {
                    element.masonry(options);

                    element.on("$destroy", function () {
                        element.masonry('destroy')
                    });

                    if (options.model) {
                        scope.$apply(function() {
                            scope.$watchCollection(options.model, function (_new, _old) {
                                if(_new == _old) return;

                                // Wait inside directives to render
                                setTimeout(function () {
                                    element.masonry("reload");
                                });
                            });
                        });
                    }
                });
            };
        }
    };
})
Run Code Online (Sandbox Code Playgroud)