AngularJS中的lightGallery(jQuery插件)

GTH*_*ten 2 javascript jquery jquery-plugins angularjs lightgallery

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

我发现一些答案表明我需要一个指令,所以我创建了以下内容:

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

然后在我看来我这样做:

<ul lightGallery>
    <li ng-repeat="photo in album.photos" data-src="{{photo.fullres}}">
        <img ng-src="{{photo.thumbnail}}" />
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

(我也试过<ul light-gallery>)当我运行页面时,当我点击任何缩略图时没有任何反应.不过,我可以alert()在链接功能中添加一个,然后显示.

如何让AngularJS与jQuery和这个插件一起玩?

更新:

经过一些调试后,似乎jQuery(element).lightGallery()在模型绑定到视图之前执行.那么接下来的问题是,当一切都被约束而不是之前,我如何获得一个被调用的指令.

Clr*_*Clr 11

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

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

        // ng-repeat is completed
        element.parent().lightGallery();
      }
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

HTML

<ul>
  <li lightgallery ng-repeat="photo in photos" data-src="{{photo.fullres}}">
    <img ng-src="{{photo.thumbnail}}" />
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

这是demo plnkr