0

I have this json. How do I display the img/n.jpg in angularjs? I have multiple images under .

bb.json

{
  "bb": [
{ 
    "chapter": "1", 
    "images": ["img/1.JPG", "img/2.jpg"]
},{ 
    "chapter": "2", 
    "images": ["img/2.jpg", "img/3.jpg"]
},{ 
    "chapter": "3", 
    "images": ["img/3.jpg", "img/4.jpg"]
}
  ]
}

app.js

$http.get('js/bb.json').success(function(data){

    $scope.images = data.bb;
    $ionicSlideBoxDelegate.$getByHandle('image-viewer').update();
})

templates.html

<ion-slide-box pager-click="navSlide(index)" delegate-handle="image-viewer" show-pager='true' does-continue="true" on-slide-changed="slideHasChanged($index)">
            <ion-slide ng-repeat="slide in images.image" >
                <div style="background: url({{slide.images}}) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; height: 100%; width: 100%;"></div>
            </ion-slide>
        </ion-slide-box>
1
  • 1
    Where do you expect the client app to get the images from? Commented Jul 15, 2015 at 3:18

2 Answers 2

1

You should iterate through the images array, and push each image to a newly created array, which is bound to your $scope.

For ex:

angular.module('fiddle', [])

.controller('MainCtrl', function ($scope) {

    var json = {
        "bb": [
            {
                "chapter": "1",
                "images": ["img/1.JPG", "img/2.jpg"]
            }, {
                "chapter": "2",
                "images": ["img/2.jpg", "img/3.jpg"]
            }, {
                "chapter": "3",
                "images": ["img/3.jpg", "img/4.jpg"]
            }
        ]
    };

    $scope.imagesArray = [];

    json.bb.forEach(function (item) {
        item.images.forEach(function (image) {
            $scope.imagesArray.push({
              src: image
            });
        });
    })

});

And in your template (obviously you would not use a <p>):

    <p ng-repeat="image in imagesArray">{{ image.src }}</p>

Plunk: http://plnkr.co/edit/Nf1wNF6M8RB2TvgkpgXe?p=preview

Sign up to request clarification or add additional context in comments.

Comments

0

you should use ng-style directive to apply background image.

<div ng-style="{'background': 'url({{slide.images}}) no-repeat center center fixed', '-webkit-background-size': 'cover', '-moz-background-size': 'cover', '-o-background-size': 'cover', 'background-size': 'cover', 'height': '100%', 'width': '100%'}"></div>

Comments

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.