【发布时间】:2013-05-13 18:13:08
【问题描述】:
我知道这已经被问了一千次了,但我想我已经尝试了我读过的每一个解决方案,但我似乎无法让它发挥作用。
我有一个 API 从中获取图像,在将图像拉入视图后,我希望它们使用 photoswipe 在移动设备上显示。
我有一个指令:
'use strict';
App.directive('photoswipe', function () {
return {
replace: false,
restrict: 'A',
link: function photoSwipeLink(scope, element, attr) {
scope.$watch(attr.photoswipe, function(value){
angular.element('#gallery a').photoSwipe({
enableMouseWheel: false,
enableKeyboard: false
});
});
}
};
});
我在控制台中收到此错误:
Code.PhotoSwipe.createInstance: No images to passed.
我猜这是因为指令在视图渲染之前运行。
如果我添加一个 $timeout 而不是 watch,那么代码就可以工作。不幸的是,这个解决方案并不好,因为从 API 获取数据可能会有延迟。此外,代码在超时内不起作用。
我试过了,上面的代码,我试过使用$viewcontentloaded,试过在ng-repeat的最后一个元素之后触发函数,但都不管用。
任何帮助将不胜感激。
编辑:
这是 HTML。
<h1>{{ gallery.headline }}</h1>
<ul id="gallery" photoswipe>
<li class="gallery" ng-repeat="image in gallery.images">
<a href="{{ image.url }}" alt="image.caption">
<img ng-src="{{ image.thumb_url }}" class="thumb" alt="{{ image.caption }}" style="display: block;">
<h3 class="headline">{{ image.caption }}</h3>
</a>
</li>
</ul>
【问题讨论】:
-
#gallery a是什么? -
这个指令是在哪里添加的?你能用指令发布你的 HTML 吗?
标签: angularjs angularjs-directive photoswipe