3

This is probably a very obvious question, but I couldn't find an answer anywhere.

I am just trying to load some JSON data from my server into the client.

How do I get all three slides in my view with angularJs? Help will be much appreciated. As it stands, with this code, I only get one of the slides pulled in.

VIEW:

          <div ng-app="app">
            <div ng-controller="CarouselDemoCtrl" id="slides_control">
              <carousel interval="myInterval">
                <slide ng-repeat="slide in slides" active="slide.active">
                  <div class="slide"><h2 class="slide-text">{{slide.Carousel[0]}}</h2></div>
                </slide>
              </carousel>
            </div>
            </div>

CONTROLLER:

angular.module('app')

.controller('CarouselDemoCtrl', function($scope, dataService) {

$scope.addSlide = function() {
    var newSlide = {"Carousel": $scope.values}
    $scope.slides.push(newSlide);
};

$scope.myInterval = 3000;

dataService.getSlides(function(response) {
        console.log(response.data);
        $scope.slides = response.data;
    });

})

SERVICE:

angular.module('app')

.service('dataService', function($http) {

this.getSlides = function(callback) { 
    $http.get('json/be-creative.json')
    .then(callback)
}

});

JSON:

[
{"AboutUs": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."},
{"Carousel": ["Slide 1", "Slide 2", "Slide 3"]}
]
2
  • It should be like to this ng-repeat="slide in slides[1].Carousel" Commented Aug 8, 2017 at 3:30
  • and <div><h2 class="slide-text">{{slide}}</h2></div> Commented Aug 8, 2017 at 3:40

1 Answer 1

2

You are binding object on the wrong level.

If you intend to use slide to represent carousel, then bind it correctly like this

dataService.getSlides(function(response) {
    console.log(response.data);
    $scope.aboutUs = response.data.AboutUs;
    $scope.slides = response.data.Carousel;
});

<slide ng-repeat="slide in slides" active="slide.active">
    <div class="slide"><h2 class="slide-text">{{slide}}</h2></div>
</slide>

Hint: ng-repeat should be used on array


Edit: Ok upon further inspection you have an additional layer of object, so find is added to locate the target object.

If possible, change the json format to

{
    "AboutUs": "...",
    "Carousel": ["Slide 1", "Slide 2", "Slide 3"]
}

angular.module('app', [])

  .controller('CarouselDemoCtrl', function($scope, dataService) {

    $scope.addSlide = function() {
      var newSlide = {}
      $scope.slides.push(newSlide);
    };

    $scope.myInterval = 3000;

    dataService.getSlides(function(response) {
      // console.log(response.data);
      $scope.aboutUs = response.data.find(function(d) { return d.AboutUs; }).AboutUs;
      $scope.slides = response.data.find(function(d) { return d.Carousel; }).Carousel;
    });

  })

  .service('dataService', function($http) {

    this.getSlides = function(callback) {
      //$http.get('json/be-creative.json')
      //  .then(callback)
      
      var json = [
{"AboutUs": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."},
{"Carousel": ["Slide 1", "Slide 2", "Slide 3"]}]
      callback({data: json});
    }

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="CarouselDemoCtrl" id="slides_control">
    <carousel interval="myInterval">
      <slide ng-repeat="slide in slides" active="slide.active">
        <div class="slide">
          <h2 class="slide-text">{{slide}}</h2>
        </div>
      </slide>
    </carousel>
  </div>
</div>

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

1 Comment

Hi. That didn't work. Are you sure about this: $scope.aboutUs = response.data.AboutUs; $scope.slides = response.data.Carousel;

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.