Image loaded event in for ng-src in AngularJS
Here is an example how to call image onload http://jsfiddle.net/2CsfZ/2/
Basic idea is create a directive and add it as attribute to img tag.
JS:
app.directive('imageonload', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('load', function() {
alert('image is loaded');
});
element.bind('error', function(){
alert('image could not be loaded');
});
}
};
});
HTML:
<img ng-src="{{src}}" imageonload />
onload image event in AngularJS component
For something like this, I would use a directive as opposed to a component. Directly from the AngularJS documentation:
When not to use Components:
-for directives that rely on DOM manipulation, adding event listeners etc, because the compile and link functions are unavailable
AngularJS ng-src does not load new image with same url
I think this is a browser caching issue, not ng-src problem.
Try appending ?<timestamp>
to value of user.profileImage.
This will force browser to fetch the new image.
AngularJS - Image onload directive - this' reference not working?
Created a fiddle for you.. checkout this
scope.$apply(function (){
//scope.$eval(attrs.ngImageOnLoad);
$parse(attrs.ngImageOnLoad)(scope, {'this': element[0]});
});
Detecting an image is fully loaded from a directive, where the directive is not applied to the image element
Check this working demo: JSFiddle
Use anguler.element
to find the img
element and bind the event.
In your directive, it should be element.find('img')
.
angular.module('Joy', [])
.directive('hello', [function () {
return {
restrict: 'A',
link: function (scope, ele) {
var img = ele.find('img');
console.log(img);
img.bind('load', function () {
console.log('img is loaded');
});
}
};
}]);
HTML:
<div ng-app="Joy">
<div hello>
<img ng-src="https://www.google.com.hk/images/srpr/logo11w.png">
</div>
</div>
Update 1
If use ng-repeat
, add a $timeout
to wait for ng-repeat
finishes first. Check working demo: JSFiddle.
Note: this demo is loading a very large image. After the image is loaded, the text img is loaded
will be shown.
Update 2
If the event binding is not working, try native image.onload
binding (or oncomplete
to find images cached). Working demo: JSFiddle.
AngularJS - Image onload event
Here's a re-usable directive in the style of angular's inbuilt event handling directives:
angular.module('sbLoad', [])
.directive('sbLoad', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var fn = $parse(attrs.sbLoad);
elem.on('load', function (event) {
scope.$apply(function() {
fn(scope, { $event: event });
});
});
}
};
}]);
When the img load event is fired the expression in the sb-load attribute is evaluated in the current scope along with the load event, passed in as $event. Here's how to use it:
HTML
<div ng-controller="MyCtrl">
<img sb-load="onImgLoad($event)">
</div>
JS
.controller("MyCtrl", function($scope){
// ...
$scope.onImgLoad = function (event) {
// ...
}
Note: "sb" is just the prefix I use for my custom directives.
Related Topics
How to Remove the "No File Chosen" Tooltip from a File Input in Chrome
Run a Website in Fullscreen Mode
How to Use JSON File in HTML Code
Download Canvas as Png in Fabric.Js Giving Network Error
Google Maps API V3: Infowindow Not Sizing Correctly
Sort an HTML List with JavaScript
How to Dynamically Scale Text Size Based on Browser Width
How to Check If the User Can Go Back in Browser History or Not
Angularjs: Better Way to Watch for Height Change
Why Don't Audio and Video Events Bubble
HTML Table with Fixed Headers and a Fixed Column
Refresh a Div That Has a Google Ad Inside It
How to Draw a Line Between Two Divs
How to Add Custom HTML Attributes in Jsx