Reputation: 15310
I have this HTML code:
<li data-thumb="images/ss.jpg">
<img src="images/ss.jpg"/>
</li>
<li data-thumb="images/ss1.jpg">
<img src="images/ss1.jpg"/>
</li>
<li data-thumb="images/ss2.jpg">
<img src="images/ss2.jpg"/>
</li>
<li data-thumb="images/ss3.jpg">
<img src="images/ss3.jpg"/>
</li>
And I wish to use my angularJS
controller in order to create something like this in my HTML instead:
<li data-thumb="{{image[i].src[i]}}" ng-repeat="image in images">
<img ng-src="{{image[i].src]i]}}"/>
</li>
(I know the above code doesn't work, that is why I am asking...)
So essentially I will have the same output as before (4 different images). My "ListingCtrl" controller so far has this:
$scope.images = [{
src1: 'images/ss.jpg',
src2: 'images/ss1.jpg',
src3: 'images/ss2.jpg',
src4: 'images/ss3.jpg',
}];
I will appreciate any help in making me accomplish this! In the future I want to parse a JSON file there and get the images from that instead... But I guess this should be a start.
Upvotes: 0
Views: 980
Reputation: 2148
use this::
<li data-thumb="{{ images[$index][key] }}" ng-repeat="(key, image) in images track by $index">
<img ng-src="{{ images[$index][key] }}"/>
</li>
Upvotes: 2
Reputation: 8971
Your images array just contains one object. If this is the data stucture, then iterate over the first element of your array in a key value fashion:
<div ng-repeat="(k, v) in images>"
<img ng-src="{{v}}"/>
</div>
Ideally, your images variable should either be an array or an object. The current data structure doesn't make sense.
Upvotes: 1
Reputation: 901
From what I can see you have an array of objects. So to print the image list do this:
<li data-thumb="{{ images[$index][key] }}" ng-repeat="(key, image) in images track by $index">
<img ng-src="{{ images[$index][key] }}"/>
</li>
or a simpler version
<li data-thumb="{{ image }}" ng-repeat="(key, image) in images track by $index">
<img ng-src="{{ image }}"/>
</li>
If you need to get the index of the image inside the array just print out or use the $index variable like so:
{{ $index }}
Upvotes: 2