How to bind the src of an image to ng-model and extract it in Angular?

可紊 提交于 2019-12-04 04:06:26
Saulo Lozano

You could do it 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 have to define selectedImg as an object in your controller like this:

$scope.selectedImg = {};

To answer your question, according to Angular Docs, you can only bind inputs, selects and textareas with ng-model, or a custom form control.

What you probably want to do is this: (which is just 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" >

So you can't really bind an img with an ng-model that way. Besides, if you could put an ng-model inside of an ng-repeat you would get the same "model value" in all of the repeated values of the ng-repeat collection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!