Angular binding with json in view

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

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

How do I get all three slides in my view using angularJs? Help would be much appreciated. Anyway, with this code I only get one of the slides.

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 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"]}
]

      

+3


source to share


1 answer


You are binding the object at the wrong level.

If you are going to use a slide to represent a carousel then link 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 in an array




Edit: Ok on further checking, you have an extra object layer, so a search is added to find the target.

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 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>
      

Run codeHide result


+2


source







All Articles