javascript - AngularJS - how to create deferred object from image object -
I am trying to create a deferred object from simple image creation like below:
Var image = new image (); Image.src = 'Some URL'; Image.onload = function () {// some code}
Now ... how to make a deferred object using the $ q library inside AngularJS? Is this possible?
I think there is some factory, where you are planning to put this function. Then:
.factory ('utils', function ($ q) {return} {createimage: function (src) {var deferred = $ q.defer (), image = new Image ( ); Image.onload = function () {deferred.resolve (image);}; image.src = src; refund retro.promis;}};});
Then you'll use it as usual:
utils.createImage ('/ assets / images / big-logo.png'). Then (function (imgobb) {console.log (imgObj);}); New syntax is similar to the original creator from
Angular 1.3 Anjor 1.3 version word service $ q
. Again the above function can be rewritten:
createImage: function (src) {return $ q (function (cancellation, reject) {var image = new Image (); image Onload = function () {resolution (image);}; image.src = src;}); }
You can use both versions with 1.3.
And finally the simple demo :
Comments
Post a Comment