3 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
我對(duì)此做了一些修改,以便$scope可以調(diào)用自定義方法:
<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()函數(shù)將是$ scope方法

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
剛剛更新了以前的代碼。
<img ng-src="{{urlImg}}" imageonload="myOnLoadImagenFunction">
和指示...
.directive('imageonload', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('load', function() {
scope.$apply(attrs.imageonload)(true);
});
element.bind('error', function(){
scope.$apply(attrs.imageonload)(false);
});
}
};
})
添加回答
舉報(bào)