0

Below is a sample JSON data getting from a service request.

 { 
   "sunday": [
    {
      "endTime": "08:50 AM",
      "startTime": "08:00 AM",
      "duration": "50",
      "period": "A"
    },
    {
      "endTime": "08:50 AM",
      "startTime": "08:00 AM",
      "duration": "50",
      "period": "A"
    }
  ]
}

This is my html code for startTime (of course there are other values too)

 <td ng-repeat="d in data.sunday.startTime track by $index">{{ d }}</td>

Controller Code

      $http.get('https://url')
        .success(function(response){
            $scope.data = response;
            console.log($scope.data);
       });

I am trying to display the data in a table format but I am not able to do it. When I console I could see the value but not in frontend table structure. Is it something to do with JSON structure or with my html?

Here is the plunker.

1
  • I'm not entirely sure what you're trying to achieve, but assuming you want to display as many columns as there are startTimes on sunday, update your HTML like so: <td ng-repeat="d in data.sunday track by $index">{{ d.startTime }}</td> Commented Dec 28, 2016 at 14:02

6 Answers 6

1

Change this:

<td ng-repeat="d in data.sunday.startTime track by $index">{{ d }}</td>

to

<td ng-repeat="d in data.sunday track by $index">{{ d.startTime }}</td>

Will work.

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

2 Comments

Hi do I have to use "track by $index" since it works without this too.
For you, no need actually. It's for unique identifier if you don't have.
1

data.sunday.startTime is a string, and you're trying to iterate over it with ngRepeat. data.sunday is an array that you can iterate over, and display the contents of.

Examples:

<tr ng-repeat="s in data.sunday track by $index">
    <td ng-repeat="(key,value) in d">{{value}}</td>
</tr>

<tr ng-repeat="s in data.sunday track by $index">
    <td>{{s.endTime}}</td>
    <td>{{s.startTime}}</td>
    <td>{{s.duration}}</td>
    <td>{{s.period}}</td>
</tr>

5 Comments

Hey if i just add this line <td ng-repeat="d in data.sunday track by $index">{{ d.startTime }}</td> it is workiing. Can you explain why you said what you said?
@HebleV Sure, which part would you like clarification on?
I see you use this <td ng-repeat="(key,value) in d">{{value}}</td> but won't it be simple to just add this line <td ng-repeat="d in data.sunday track by $index">{{ d.startTime }}</td>
@HebleV That definitely works, but I didn't want to assume you only wanted to display one property from each element, and I also wanted to give more general examples from which you could take the pieces that you like. The way that you've just described will definitely work for displaying just the startTime values, but my examples were meant to show how to do it, whether you're displaying whole or partial objects, and give the core syntax for ngRepeat.
Hey thanks for the explanation. That worked me the way I wanted besides yours too. Upvoted
0

You have to do:

<td ng-repeat="d in data.sunday track by $index">{{ d.startTime }}</td>

Comments

0

try this:

<td ng-repeat="d in data.sunday track by $index">{{ d.startTime }}</td>

1 Comment

It would be nice if you could add some explanation to your answer. Currently your answer looks a bit like a wild guess...
0

You should go over the attributes of data.sunday in the ng-repeat

<td ng-repeat="d in data.sunday track by $index">
  Start time: {{ d.startTme }}
  End time: {{ d.endTime }}
  Period: {{ d.period }}
  Duration: {{ d.duration }}
</td>

Here is a working version of your Planker: https://embed.plnkr.co/A4kOM7I0gJVOEoq6zgXT/

Comments

0

This will work for you.

    <tr ng-repeat="d in data.sunday track by $index">

            <td>{{d.startTime}}</td>
            <td>{{d.endTime}}</td>
            <td>{{d.period}}</td>
            <td>{{d.duration}}</td>
   </tr>

Checkout my plunker for your reference

https://plnkr.co/edit/Mv4HLF1JyCHK10vIk4q4?p=preview

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.