AngularJS 中的 lightGallery(jQuery 插件)

lightGallery (jQuery plugin) in AngularJS(AngularJS 中的 lightGallery(jQuery 插件))
本文介绍了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 插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to sort a array of object based on two properties and check the rage is sequential?(如何根据两个属性对对象数组进行排序,并检查范围是否连续?)
Untyped function calls may not accept type arguments - Angular 5 http calls(非类型化函数调用可能不接受类型参数-角度5 http调用)
ReferenceError: Excel is not defined(ReferenceError:未定义Excel)
How to save two canvas(outer and inner) as png using fabric js in angular(如何保存两个画布(外部和内部)为PNG使用织物js的角度)
Access to #39;Set-Cookies#39; header in $http(访问$http中的Set-Cookies标头)
Adding lt;stronggt;/bold text in translated string using angular-translate(使用角度翻译在翻译后的字符串中添加lt;stronggt;/粗体文本)