0

我正在尝试在 angularJS 应用程序中实现打包。

当我一一手动定义项目时(请参阅注释的 html 代码),Packery 工作正常。当我尝试用 ng-repeat 循环做同样的事情时,packery 不起作用。

如何在 angularJS 模板中使用 ng-repeat 使 packery 工作?

模板:

<div class="item-container" workspace>

  <div class="module-sizer"></div>
  <div class="gutter-sizer"></div>

  <div
    ng-repeat="item in items"
    class="item">
         <!--my {{angular content}} here-->
  </div>

  <!-- this works: -->
  <!--
  <div class="item">
    item 1
  </div>
  <div class="item">
    item 2
  </div>
  <div class="item">
    item 3
  </div>
  <div class="item">
    item 4
  </div>
  <div class="item">
    item 5
  </div>
  <div class="item">
    item 6
  </div>
  <div class="item">
    item 7
  </div>
  -->

</div>

角度指令,基于: http ://codepen.io/gruntruk/pen/Cpewt/

myApp.directive('workspace', ['$rootScope', function ($rootScope) {
    return {

        constrain: 'A',

        link: function (scope, element, attrs) {

            element.ready(function () {

                var packery = new Packery(element[0], {
                    rowHeight: '.module-sizer',
                    itemSelector: '.item',
                    columnWidth: '.module-sizer'
                });

                angular.forEach(packery.getItemElements(), function (item) {
                    var draggable = new Draggabilly(item);
                    packery.bindDraggabillyEvents(draggable);
                });

                packery.layout();

            });

        }
    };
}]);
4

1 回答 1

0

好的,我找到了答案。

1)我必须使用带有控制器接收事件的指令

2)我必须添加一个 setTimeout。

app.directive('lastRepeat', function () {
    return function(scope, element, attrs) {
        if (scope.$last) setTimeout(function(){
            scope.$emit('LastElement', element, attrs);
        }, 1);
    };
});
于 2014-10-30T10:34:45.090 回答