user6767148 user6767148 - 1 year ago 91
HTML Question

Change ng-src value onclick

I've an AngularJS page and I want it to change ng-src value of an element when clicking on another element. I intialized variable src:


Then I made this element:

<img data-ng-src="{{src}}/1.jpg" id="img1">

And finally I want when someone clicks on my link, It change that src to
, So I tried this:

<a href="javascript:void(0)" data-ng-click="selectImage('lists/1.html');document.getElementById('img1').setAttribute('data-ng-src', '{{src}}/1a.jpg');"></a>

(Don't care about my empty link, I know how to click on it...), My problem is, The value of src doesn't change and My page is still the first image, How can I improve my code to change value

Answer Source

Create new varibale image

data-ng-init="src='assets/img/projects/1'; image='1.jpg'"

Then HTML Element

<img data-ng-src="{{src}}/{{image}}" id="img1">

If someone clicks on you link the change the image value

<a href="javascript:void(0)" data-ng-click="selectImage('lists/1.html'); image='1a.jpg'"></a>

Hope this helps ! Thanks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download