2

I'm new to Angular. What I want to do is display data from an array of objects on the page and provide buttons for the user to move on the next/previous array item.

Let's say our array is:

var destinations = [
    {
      name: "Rome",
      weather: "sunny",
      price: "$2999"
    },
    {
      name: "Paris",
      weather: "cloudy",
      price: "$4500"
    }
];

If I want to display it in HTML, I can write {{destinations[1].name}} and it works, but how do I make this dynamic? I tried defining an i variable in the controller and inserting [i] into the HTML directive, but that doesn't work.

How can I make this work?

5
  • most tutorials will show you how to use ng-repeat. Should really do more research before asking here Commented May 14, 2016 at 17:16
  • It's a fine question. Sometimes you don't even know what to search on to find ng-repeat. Commented May 14, 2016 at 17:18
  • Whoa I just realized your question has nothing to do with repeating... updating my answer Commented May 15, 2016 at 3:08
  • Updated answer should really answer the question. Commented May 15, 2016 at 3:13
  • Great, thank you @doug65536! I knew about ng-repeat, but didn't use it since I only wanted one instance displayed at a time. Commented May 15, 2016 at 18:51

4 Answers 4

2

in you html use ng-repeat

<div ng-repeat="destination in destinations">{{destination}}</div>

or in a controller you can use

angular.forEach(destinations,function(value) {
//here value is equal to one elment of your array
alert(value);
);
Sign up to request clarification or add additional context in comments.

Comments

1

Use a member of the scope to represent the current item.

HTML

<div data-ng-app="exampleModule" data-ng-controller="exampleController">
  <div>
    <button data-ng-click="changeDestination(1)">
      Next
    </button>
    <button data-ng-click="changeDestination(-1)">
      Prev
    </button>
  </div>
  <div class="dest-container">
    <div class="dest-field dest-price-field">  
      <div class="dest-title">Price:</div>
      <div class="dest-content">{{ current.price }}</div>
    </div>
    <div class="dest-field dest-name-field">  
      <div class="dest-title">Name:</div>
      <div class="dest-content">{{ current.name }}</div>
    </div>

    <div class="dest-field dest-weather-field">  
      <div class="dest-title">Weather:</div>
      <div class="dest-content">{{ current.weather }}</div>
    </div>
  </div>
</div>

This will create a one display, displaying $scope.current.

I have created a minimal complete example with your destination array.

Javascript

(function(angular) {
  "use strict";

  var exampleModule = angular.module('exampleModule', []);
  exampleModule.controller('exampleController', ['$scope', function($scope) {
    $scope.destinations = [
      {
        name: "Rome",
        weather: "sunny",
        price: "$2999"
      },
      {
        name: "Paris",
        weather: "cloudy",
        price: "$4500"
      }
    ];

    $scope.currentItem = 0;
    $scope.current = $scope.destinations[$scope.currentItem];

    $scope.changeDestination = function(diff) {
      $scope.currentItem += diff;

      if ($scope.destinations.length) {
        while ($scope.currentItem >= $scope.destinations.length)
          $scope.currentItem -= $scope.destinations.length;
        while ($scope.currentItem < 0)
          $scope.currentItem += $scope.destinations.length;
      } else {
        $scope.currentItem = 0;
      }
      $scope.current = $scope.destinations[$scope.currentItem];
    };
  }]);
}(angular));

The page loads AngularJS 1.4.8 before running the javascript code.

The same thing can be used for a range of items, you can slice the array and set an array in $scope.current then use ng-repeat to show the items to implement pagination.

Comments

1
 <div ng-repeat="value in destinations">
       <p>
       <span>{{value.name}}</span>
       <span>{{value.weather}}</span>
       <span>{{value.price}}</span>
       </p>
 </div>

Comments

-1
<div ng-repeat="dest in destinations">
{{name}}
</div>

Would be one way, remember destinations must be part of the scope of your controller

1 Comment

{{name}} won't work here... you'd need {{dest.name}}.

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.