AngularJS中ng-src中的图像加载事件

我有看起来像的图片<img ng-src="dynamically inserted url"/>。加载单个图像时,我需要应用iScroll refresh()方法以使图像可滚动。

知道何时完全加载映像以运行某些回调的最佳方法是什么?


沧海一幻觉
浏览 936回答 3
3回答

千万里不及你

我对此做了一些修改,以便$scope可以调用自定义方法:<img ng-src="{{src}}" imageonload="doThis()" />指令:.directive('imageonload', function() {        return {            restrict: 'A',            link: function(scope, element, attrs) {                element.bind('load', function() {                    //call the function that was passed                    scope.$apply(attrs.imageonload);                });            }        };    })希望有人觉得它非常有用。该doThis()函数将是$ scope方法

三国纷争

刚刚更新了以前的代码。<img ng-src="{{urlImg}}" imageonload="myOnLoadImagenFunction">和指示...&nbsp; &nbsp; .directive('imageonload', function() {&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restrict: 'A',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link: function(scope, element, attrs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element.bind('load', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope.$apply(attrs.imageonload)(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element.bind('error', function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope.$apply(attrs.imageonload)(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; })
打开App,查看更多内容
随时随地看视频慕课网APP