2

I get data from URL and I have this data in result :

array: 2[
    1 => "LA"
    2 => "NY"
 ]

I want to show only value in List . Here is my code :

var app = angular.module('myApp', []);
        app.controller('MyCtrl', function($scope, $http) {
            $http.get('/getCities').
            success(function(data, status, headers, config) {
                $scope.cities = data;
            }).
            error(function(data, status, headers, config) {
                // log error
            });
        });

Is it the best way to show them?

<ul ng-repeat="(key, value) in cities">
  <li>  {{ value }} </td>
</ul>

EDITED : I edit my html to :

                            <ul ng-repeat="city in cities">
                                <li>  {{ city }} </li>
                            </ul>

Actually , I give this error : angular.min.js:117 Error: [ngRepeat:dupes]

5
  • Does it work? If so, then why ask the question? Commented May 18, 2016 at 6:21
  • Also, please read docs.angularjs.org/api/ng/service/$http#deprecation-notice Commented May 18, 2016 at 6:22
  • What exactly is the data structure returned? What you have at the top of your question isn't anything understandable by JavaScript. Commented May 18, 2016 at 6:28
  • Are you getting a json response? Commented May 18, 2016 at 6:40
  • @Phil No , It doesn't Commented May 19, 2016 at 19:15

1 Answer 1

1

This is an array. You don't need key value notations for repeatation. Just write

<ul ng-repeat="city in cities">
  <li>  {{ city }} </td>
</ul>

and Thats all.

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

5 Comments

How do you know it's an array? Seems to be more akin to a map with keys "1" and "2", ie {"1":"LA","2":"NY"}. Until OP confirms, we're all just guessing
Well it is shown above in square bracket notation and it has array written before it. either of this if it were an object the above code should work fine for the repeat in the markup.
I am also guessing. :)
I get this error : angular.min.js:117 Error: [ngRepeat:dupes]
This error occur when duplicates are being used in repeats. You can use <ul ng-repeat="city in cities track by $index"> to avoid it. But it is not recommended until you want to allow duplicates.

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.