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


基础教程推荐
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01