简体   繁体   English

AngularJS中的lightGallery(jQuery插件)

[英]lightGallery (jQuery plugin) in AngularJS

I'm trying to get the lightGallery jQuery plugin ( http://sachinchoolur.github.io/lightGallery/index.html ) to work with AngularJS. 我正在尝试使用lightGallery jQuery插件( http://sachinchoolur.github.io/lightGallery/index.html )与AngularJS一起使用。

I found some answers that suggested I needed a directive, so I created the following: 我发现一些答案表明我需要一个指令,所以我创建了以下内容:

.directive('lightGallery', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            jQuery(element).lightGallery();
        }
    };
})

Then in my view I do this: 然后在我看来我这样做:

<ul lightGallery>
    <li ng-repeat="photo in album.photos" data-src="{{photo.fullres}}">
        <img ng-src="{{photo.thumbnail}}" />
    </li>
</ul>

(I also tried with <ul light-gallery> ) When I run the page nothing happens when I click any of the thumbnails. (我也试过<ul light-gallery> )当我运行页面时,当我点击任何缩略图时没有任何反应。 I can put an alert() in the link function, though, and that is displayed. 我可以在链接功能中放置一个alert() ,然后显示。

How can I get AngularJS to play along with jQuery and this plugin? 如何让AngularJS与jQuery和这个插件一起玩?

UPDATE: 更新:

After some debugging it seems that jQuery(element).lightGallery() is executed before the model is bound to the view. 经过一些调试后,似乎在模型绑定到视图之前执行了jQuery(element).lightGallery() So the question then is how I can get a directive to be called when everything is bound and not before. 那么接下来的问题是,当一切都被约束而不是之前,我如何获得一个被调用的指令。

Call lightGallery once ng-repeat is finished rendering. 一旦ng-repeat完成渲染,调用lightGallery。
You can just modify the directive like this 您可以像这样修改指令

.directive('lightgallery', function() {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      if (scope.$last) {

        // ng-repeat is completed
        element.parent().lightGallery();
      }
    }
  };
});

HTML HTML

<ul>
  <li lightgallery ng-repeat="photo in photos" data-src="{{photo.fullres}}">
    <img ng-src="{{photo.thumbnail}}" />
  </li>
</ul>

Here is the demo plnkr 这是demo plnkr

Using a combination of 2 directives allows you to specify a parent via a directive, as well as pass in options to a scoped variable, which provides the opportunity for more customization of Light Gallery. 使用2个指令的组合,您可以通过指令指定父级,以及将选项传递给范围变量,从而为Light Gallery提供更多自定义的机会。 The secondary directive triggers the parent to bind (using the same idea presented in @Clr's solution) 辅助指令触发父绑定(使用@ Clr解决方案中提供的相同想法)

This directive is for the parent element, and you may pass a galleryOptions scope variable which is simply passed along when binding Light Gallery: 该指令适用于父元素,您可以传递一个galleryOptions范围变量,该变量在绑定Light Gallery时简单传递:

.directive('lightGallery', function() {
    return {
        restrict: 'A',
        scope: {
            galleryOptions: '='
        },
        controller: function($scope) {

            this.initGallery = function() {
                $scope.elem.lightGallery($scope.galleryOptions);
            };

        },
        link: function(scope, element, attr) {
            scope.elem = element;
        }
    }
})

This directive is for each "item" in the Light Gallery: 该指令适用于Light Gallery中的每个“item”:

.directive('lightGalleryItem', function() {
    return {
        restrict: 'A',
        require: '^lightGallery',
        link: function (scope, element, attrs, ctrl) {

            if (scope.$last) {
                ctrl.initGallery();
            }

        }
    }
})

The resulting markup (which can really be anything you'd like by specifying the selector option when initializing Light Gallery): 生成的标记(通过在初始化Light Gallery时指定selector选项,它可以是您真正喜欢的任何内容):

<ul light-gallery gallery-options="galleryOptions">
    <li ng-repeat="photo in album.photos" data-src="{{photo.fullres}}" light-gallery-item>
        <img ng-src="{{photo.thumbnail}}" />
    </li>
</ul>

Whereas options may be any valid Light Gallery options, such as: 选项可以是任何有效的Light Gallery选项,例如:

$scope.galleryOptions = {
    selector: '.file-trigger',
    ...
};

without directive.. 没有指令..

HTML HTML

<ul id="lightgallery">
  <li ng-repeat="image in album.images" data-src="{{imagem.fullres}}">
    <img ng-src="{{image.thumbnail}}" />
  </li>
</ul>

JavaScript JavaScript的

// $http, Restangular whatever to return data in album.images

// unbind before..
jQuery('#lightgallery').unbind().removeData();

// $timeout after
$timeout(function () {
  jQuery('#lightgallery').lightGallery();
}, 100);

Works for me.. 对我有用..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM