0

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.

3 Answers 3

2

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 }}
Sign up to request clarification or add additional context in comments.

Comments

2

use this::

<li data-thumb="{{ images[$index][key] }}" ng-repeat="(key, image) in images track by $index">
    <img ng-src="{{ images[$index][key] }}"/>
</li>

3 Comments

Unfortunately it does not work :( the images are "displayed" but are empty (i can't actually see them)
then there must be no images at that url.
How could this be if beforehand it showed the images? (When i just used the normal <img src="images/ss.jpg"/> )
1

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.

4 Comments

You are right, I meant for it to be an array of 4 images (4 entries) and not an array of a 4-image-object. I changed the controller code to: $scope.images = { src1: 'images/ss.jpg', src2: 'images/ss1.jpg', src3: 'images/ss2.jpg', src4: 'images/ss3.jpg', }; Any sense now?
Yes, now just replace 'images[0]' by 'images' in my answer.
Sadly it still does not work.. It even "messed up" my angular for other things apparently..Can you provide with the full 2 lines of code that you meant? Because you only posted part of the lines.
Thanks for all your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.