AngularJS 中的 lightGallery(jQuery 插件)

时间:2023-03-26
本文介绍了AngularJS 中的 lightGallery(jQuery 插件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 lightGallery jQuery 插件 (http://sachinchoolur.github.io/lightGallery/index.html) 与 AngularJS 一起工作.

I'm trying to get the lightGallery jQuery plugin (http://sachinchoolur.github.io/lightGallery/index.html) to work with 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>

(我也试过 <ul light-gallery>)当我运行页面时,单击任何缩略图时都没有任何反应.不过,我可以在链接函数中放置一个 alert(),然后就会显示出来.

(I also tried with <ul light-gallery>) When I run the page nothing happens when I click any of the thumbnails. I can put an alert() in the link function, though, and that is displayed.

我怎样才能让 AngularJS 与 jQuery 和这个插件一起玩?

How can I get AngularJS to play along with jQuery and this plugin?

更新:

经过一些调试,似乎 jQuery(element).lightGallery() 在模型绑定到视图之前执行.所以接下来的问题是我如何在所有内容都绑定时而不是之前调用指令.

After some debugging it seems that jQuery(element).lightGallery() is executed before the model is bound to the view. So the question then is how I can get a directive to be called when everything is bound and not before.

推荐答案

在 ng-repeat 完成渲染后调用 lightGallery.
您可以像这样修改指令

Call lightGallery once ng-repeat is finished rendering.
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

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

这里是演示plnkr

这篇关于AngularJS 中的 lightGallery(jQuery 插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如果您在页面中包含 2 个版本的 jQuery,您如何将 下一篇:如何在正在编写的另一个 jQuery 插件中包含外部插

相关文章

最新文章