Image crop with AngularJS

Gal Ben-Haim picture Gal Ben-Haim · Jan 14, 2013 · Viewed 22.9k times · Source

I want to let the user crop an image, I found this JQuery plugin - http://deepliquid.com/content/Jcrop.html

I tried to use it with Angular-ui's Jquery passthrough option, adding the ui-jq=Jcrop directive to the <img>

The problem is that If I use ng-src to dynamically change the image it doesn't work and nothing is seen. If I change it to src and put a static url I can see the image and Jcrop.

how can I fix that ? also, how can I listen to Jcrop's callbacks to know what is the user's selection ?

is there a better / simpler way to add image cropping functionality to AngularJS ?

Answer

Valentyn Shybanov picture Valentyn Shybanov · Jan 14, 2013

Here is my solution:

I've written a directive that create img element and apply plugin on it. When src is changed, this img is removed and content that was created by plugin is also destroyed and then re-created new img with new src and again applied plugin on it.

Also provided 'selected' callback to be able to get coordinated that were selected (wrapped in $apply so you can modify your scope values in it).

Check my solution at Plunker