如何将图像的src绑定到ng-model并在Angular中提取它?

Rob*_*Seg 7 image angularjs angular-ngmodel

我想将图像的源绑定到另一个图像的源.

视觉例子

在最终结果中,大图像的源应绑定到点击的较小(缩略图)图像的src.这是否可以使用ng-model?

这就是我所拥有的

        <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>
Run Code Online (Sandbox Code Playgroud)

小智 11

您可以使用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>
Run Code Online (Sandbox Code Playgroud)

但您必须将selectedImg定义为控制器中的对象,如下所示:

$scope.selectedImg = {};
Run Code Online (Sandbox Code Playgroud)


cyb*_*ice 5

要回答您的问题,根据 Angular Docs,您只能使用 ng-model 或自定义表单控件绑定输入、选择和文本区域。

您可能想要做的是:(这正是 Saulo Lozano 使用 ng-click 所做的)

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

<img ng-src="{{thumb.images[0].list}}" ng-click="selectedImg.src = thumb.images[0].list" >
Run Code Online (Sandbox Code Playgroud)

所以你不能真正以这种方式将 img 与 ng-model 绑定。此外,如果您可以将 ng-model 放入 ng-repeat 中,您将在 ng-repeat 集合的所有重复值中获得相同的“模型值”。