2013-04-14 41 views
5

有没有什么办法在Angular中实现jQuery的Quicksand plugin?也许有一个实现,但我似乎无法找到它。AngularJS Quicksand

也许一个策略可以帮助我,因为流沙需要一个列表,然后接收一个参数作为新列表,但是使用Angular重新呈现数据的方式我不知道该怎么做。

+0

与任何其他DOM操作插件一样,需要在角度指令内对其进行初始化。如果angular会像'ng-repeat'一样创建'LI'元素,则将代码包装在'$ timeout'中。 – charlietfl

+0

@charlietfl,什么? – arg20

+1

什么是'什么'? – charlietfl

回答

8

我实现了类似的东西用砖石指令+ NG-动画的进入/离开动画,这里有一个CSS动画仅演示(铬供应商前缀的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"); 
           }); 
          }); 
         }); 
        } 
       }); 
      }; 
     } 
    }; 
}) 
+0

令人惊叹的指令! – arg20

+0

这很漂亮。我正在处理一个更复杂的例子,那里有LI的容器(这也是一个LI),并且动画应用到所有后代都有问题。我的修复是改变'var itemSelector = type +“:not([class $ =' - leave-active'])”;'to'var itemSelector ='.masonry>'+ type +“:not([class $ = '-leave活性'])“;' – Kywillis

0

你需要添加一个directive来做到这一点。

所以只用了jQuery,你必须:

JS

$('#source').quicksand($('#destination li')); 

HTML

<ul id="source"> 
    <li data-id="iphone">iOS</li> 
    <li data-id="android">Android</li> 
    <li data-id="winmo">Windows Phone 7</li> 
</ul> 

<ul id="destination" class="hidden"> 
    <li data-id="macosx">Mac OS X</li> 
    <li data-id="macos9">Mac OS 9</li> 
    <li data-id="iphone">iOS</li> 
</ul> 

具有角你可以这样做:

JS

yourApp.directive('jqQuicksand', function(){ 
    var linkFn = function(scope,element,attrs){ 
     // element here = $(this) 
     // bind your plugin or events (click, hover etc.) here 
     element.quicksand($(attrs.jqQuicksand)); 
    } 

    return { 
     restrict:'A', 
     scope: {}, 
     link: linkFn 
    } 
}); 

HTML

<ul data-jq-quicksand="#destination li" id="source"> 
    <li data-id="iphone">iOS</li> 
    <li data-id="android">Android</li> 
    <li data-id="winmo">Windows Phone 7</li> 
</ul> 

<ul id="destination" class="hidden"> 
    <li data-id="macosx">Mac OS X</li> 
    <li data-id="macos9">Mac OS 9</li> 
    <li data-id="iphone">iOS</li> 
</ul> 

注意,这是未经测试,但应该没问题。