How to link src images with ng model and extract it in Angular?

I want to link the source of the image to the source of another image.

visual example

Ultimately, the source of the large image must be tied to the src of the image with a thumbnail (thumbnail) with a click. Is this possible with the ng model?

Here is what i have

<div> <img ng-src="{{selectedImg.src}}"> </div> <div> <ul ng-repeat="thumb in franchises"> <li> <img ng-src="{{thumb.images[0].list}}" ng-model="selectedImg"> </li> </ul> </div> 
+6
source share
2 answers

You can do this using ng-click:

 <div> <img ng-src="{{selectedImg.src}}" alt="{{slide.images[0].list}}"> </div> <div> <ul ng-repeat="thumb in franchises"> <li> <img ng-src="{{thumb.images[0].list}}" alt="{{thumb.images[0].list}}" ng-click="selectedImg.src = thumb.images[0].list" /> </li> </ul> </div> 

But you must define selectedImg as an object in your controller as follows:

 $scope.selectedImg = {}; 
+10
source

To answer your question, according to Angular Docs, you can only bind inputs, selections, and text fields using an ng model or a custom form control.

What you probably want to do is the following: (this is exactly what Saulo Lozano did with ng-click)

https://jsfiddle.net/4fz4nx1k/2/

 <img ng-src="{{thumb.images[0].list}}" ng-click="selectedImg.src = thumb.images[0].list" > 

This way you really cannot bind img with the ng model. In addition, if you can place the ng model inside ng-repeat, you will get the same “model value” in all duplicate values ​​of the ng-repeat collection.

+4
source

Source: https://habr.com/ru/post/984906/


All Articles