我能夠使用ImagesLoaded插件,如this plunkr所示。
這僅僅是一個例子,指出你在正確的方向。理想情況下,您將提取ImagesLoaded插件的邏輯(不再需要Jquery),並在AngularJS中完成此操作。
我認爲你想以不同的方式聽取ImagesLoaded事件。這裏是修改的指令代碼w /一些評論/建議。
angular.module('myApp', []).
directive('pendingIndicator', function(){
return {
restrict: 'A',
link: function(scope, element) {
// setup the container for ImagesLoaded ... note instead of using
// this directive on an individual image, you may consider using
// it on the element that contains many images...
scope.imagesLoaded = imagesLoaded(element);
// start your progress/loading animation here
// (or whenever you attempt to load the images)
scope.imagesLoaded.on('always', function() {
console.log('always event: Triggered after all images have been either loaded or confirmed broken.');
// end the progress/loading animation here for all images or do
// it individually in the progress event handler below
});
scope.imagesLoaded.on('done', function() {
console.log('done event: Triggered after all images have successfully loaded without any broken images.');
});
scope.imagesLoaded.on('fail', function() {
console.log('fail event: Triggered after all images have been loaded with at least one broken image.');
});
scope.imagesLoaded.on('progress', function(instance, image) {
console.log('proress event: Triggered after each image has been loaded.', instance, image);
// end the progress/loading animation here or do it for all images in the always
// event handler above
});
}
};
});
我想我已經去過了。問題是我想要監視的圖像是異步加載的,而不是在頁面加載時加載的。所以我需要告訴ImagesLoaded通過一個事件開始工作。在那裏我得到了上面的錯誤。 (當我在一個事件中包裝了ImageLoaded的設置) – kernfrucht
嗨Sunil,你的指令是爲我工作的,但「總是」被稱爲n次而不是一次。如果我加載10個圖像,它被稱爲10次 – jmhostalet