AngularJS中的lightGallery(jQuery插件) [英] lightGallery (jQuery plugin) in AngularJS

查看:211
本文介绍了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插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆