theLearner theLearner - 1 year ago 40
AngularJS Question

ng-click not working, trying to replace an image on click

I'm new of Angular JS, Well I'm trying to replace an image on click. i.e.,One Click on Img1 i would replace the previous image by Img1 and so on.

To achieve this I'm using ng-click. The thing is even the image names are being retrieved from DB.

So when I do this, it doesn't work-

<img class="img-responsive" src="images/products/{{oneItem.Image3}}" ng-click="current='{{oneItem.Image3}}'" />

Whereas works when I do this-

<img class="img-responsive" src="images/products/{{oneItem.Image3}}" ng-click="current='test3.jpg'" />

And I'm trying to replace it here-

<div class="trueimagger">
<img ng-src="WebDrop/images/products/{{ current }}" />

Can someone help me out by pointing where I'm messing it up.

Please lemme know if any additional info is needed. :)

Answer Source

ngClick expects an expression:

ng-click="current = oneItem.Image3"

You should also use ngSrc directive instead of src attribute directly:

<img class="img-responsive" 
     ng-click="current = oneItem.Image3" />