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

相关文章