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

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -