angular ng-repeat and images in a row

寵の児 提交于 2019-12-11 12:08:47

问题


In my view model I have an array of exactly 4 icon names.

If I place the 4 images like so:

<img ng-src="{{'/Content/img/' + vm.indicator[0]}}" alt="" />
<img ng-src="{{'/Content/img/' + vm.indicator[1]}}" alt="" />
<img ng-src="{{'/Content/img/' + vm.indicator[2]}}" alt="" />
<img ng-src="{{'/Content/img/' + vm.indicator[3]}}" alt="" />

they appear nicely in a row as desired.

However, when I use

<div ng-repeat="indicator in vm.indicator track by $index">
    <img ng-src="{{'/Content/img/' + indicator}}" alt="" />
</div>

they appear below each other. (note: I need the "track by $index" bit because some icons have the same name).

I don't understand it. If I just place an empty div around the first code nothing changes. Why would the ng-repeat div change anything? And how can I make the two code snippets behave the same?

This is not a huge problem in my case because I always have exactly 4 icon names in my array. But what if the array was of variable length?


回答1:


Put the ng-repeat in the img tag like so

<div >
    <img ng-repeat="indicator in vm.indicator track by $index" ng-src="{{'/Content/img/' + indicator}}" alt="" />
</div>

That way you dont make multiple divs



来源:https://stackoverflow.com/questions/34279197/angular-ng-repeat-and-images-in-a-row

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