我正在尝试使用lightgallery jQuery插件(http://sachinchoolur.github.io/lightGallery/index.html)来使用AngularJS.
.directive('lightgallery', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
jQuery(element).lightgallery();
}
};
})
然后在我看来我这样做:
<ul lightgallery>
<li ng-repeat="photo in album.photos" data-src="{{photo.fullres}}">
<img ng-src="{{photo.thumbnail}}" />
</li>
</ul>
(我也试过< ul light-gallery>)
当我运行页面时,单击任何缩略图时没有任何反应.
我可以在链接功能中放置一个alert(),然后显示.
如何让AngularJS与jQuery和这个插件一起玩?
更新:
经过一些调试后,似乎在模型绑定到视图之前执行了jQuery(element).lightgallery().
那么接下来的问题是,当一切都被约束而不是之前,我如何获得一个被调用的指令.
解决方法:
一旦ng-repeat完成渲染,调用lightgallery.
您可以像这样修改指令
.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