Angularjs with Packery.js

Angularjs with Packery.js

本文关键字:js Packery with Angularjs      更新时间:2023-09-26

试图获得Packery.js使用我正在使用的angularjs应用程序。

出于某种原因,他们似乎在一起玩得不好。我以为它可能会通过isInitLayout错误的设置来解决,但是,仍然没有爱。

这是我的(引导程序 3)HTML:

<div class="row" class="js-packery" 
     data-packery-options='{ "itemSelector": ".packery-item", 
                             "gutter": 10,  
                             "columnWidth": 60, 
                             "isInitLayout": false }'>
    <artifact class="packery-item" ng-repeat="(index, thing) in data | limitObjectTo:4" thing="thing"></artifact>
</div>

我开始怀疑这是否是因为我正在使用的工件指令......

如前所述,您必须制定一个处理 Packery 使用的指令。

该指令使Packery在我正在从事的项目中与AngularJS一起工作。

工作小提琴:http://jsfiddle.net/8P6mf/2/

.HTML:

<div class="wrapper">
    <div ng-repeat="item in test" danny-packery>
        {{item.name}}
    </div>
</div>

JavaScript:

var dannyPackery = app.directive('dannyPackery', ['$rootScope', function($rootScope) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            console.log('Running dannyPackery linking function!');
            if($rootScope.packery === undefined || $rootScope.packery === null){
                console.log('making packery!');
                $rootScope.packery = new Packery(element[0].parentElement, {columnWidth: '.item'});
                $rootScope.packery.bindResize();
                $rootScope.packery.appended(element[0]);
                $rootScope.packery.items.splice(1,1); // hack to fix a bug where the first element was added twice in two different positions
            }
            else{
                $rootScope.packery.appended(element[0]);
            }
            $rootScope.packery.layout();
        }
    };
}]);

你找到的任何JS库都不会简单地与Angular一起使用。Angular 编译 DOM 会导致其他库丢失上下文。您必须编写自定义指令。

我找到了砌体的现有指令:https://github.com/passy/angular-masonry 和包装与砌体非常相似,所以我相信你可以将其用于包装。