2

I am creating project using angularjs. I have problem while showing data:

I have JSON like:

$scope.customerContracts =     [{
      "description": null,
      "latency": "1",
      "linkid": "26149e91-f6c8-47fb-a10d-691e76486871",
      "contracts": [{
        "contractid": "26149e91-f6c8-47fb-a10d-691e76486871.2",
        "duration": 7200,
        "bandwidth": 300000,
        "price": 0,
        "contractStatus": 8,
        "version": 1560,
        "deletedtimestamp": 0,
        "currencyID": "1",
        "renewal-option": 1,
        "contract-start-time": 1461918999000,
        "contract-end-time": 1461926198000
      }],

      "tag": null,
      "connections": ["49b33cd1-5722-4e8e-b40c-03c3407c5efe", "cd354769-d52a-414e-bb7c-f0e0690ddf3b"],
      "type": "0",
      "linkStatus": 6,
      "billing-id": "5f1dd7c5-c48b-44ed-be14-cf2a8730fe27"
    }]

Here is my code which is using in html.I dont know where I am going wrong:

<tr ng-repeat="contract in customerContracts.contracts">
                  <td>{{contract.contractid}}</td>
                  <td>{{contract.contractStatus}}</td>
                  <td>{{contract.bandwidth}}</td>
                  <td>{{contract["contract-start-time"]}}</td>
                          <td>{{contract["contract-end-time"]}}</td>
                  <td>{{contract["renewal-option"]}}</td>
                  <td>{{contract.price}}</td>
                  <td><span class="icon_delete" ng-click="deleteInformation()"></span></td>
                </tr>
1
  • Are you getting some error? Commented Apr 29, 2016 at 9:46

6 Answers 6

3

customerContracts being an array you'll have to ng-repeat there first and then loop over contracts.

Something like :

<div ng-repeat="contracts in customerContracts">
<...>
<tr ng-repeat="contract in contracts.contracts">
                  <td>{{contract.contractid}}</td>
                  <td>{{contract.contractStatus}}</td>
                  <td>{{contract.bandwidth}}</td>
                  <td>{{contract["contract-start-time"]}}</td>
                          <td>{{contract["contract-end-time"]}}</td>
                  <td>{{contract["renewal-option"]}}</td>
                  <td>{{contract.price}}</td>
                  <td><span class="icon_delete" ng-click="deleteInformation()"></span></td>
                </tr>
<...>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

And the problem being ? What is your problem ? No data? Error ?
Might be a stupid question but does OP have a table <table> surrounding the table-row <tr>? Otherwise this code should work.
@thepio, well we can't think that far given the question. As he/she a webserver running ? Is he/she using a browser ? :)
@MichaelLaffargue I'm sorry I don't quite understand, but my original comment was pointed towards the original question asker. And your answer should work which ever way since it was stated that the array is created in the controller etc. So given the circumstances your answer is correct.
1

Get the value in separate $scope like this,

$scope.getValue=$scope.customerContracts.contracts;

Comments

1

You are going wrong in the way you are accessing your data. $scope.customerContracts doesn't have contracts. Each object inside it has. And then again contracts is again an array of objects. So you would need to do a repeat over it again. You should have something like

<elem ng-repeat="customerContract in customerContracts">
    <tr ng-repeat="contract in customerContract.contracts">
        <! -- Your code -- >
    </tr>
</elem>

Note you would need to choose elem based upon your requirement.

Find the plunk omitting the elem.

1 Comment

Do you have a JSfiddle or something?
1

angular.module('app',[]).controller('ctrl',function($scope,$http){

$scope.customerContracts =     [{
      "description": null,
      "latency": "1",
      "linkid": "26149e91-f6c8-47fb-a10d-691e76486871",
      "contracts": [{
        "contractid": "26149e91-f6c8-47fb-a10d-691e76486871.2",
        "duration": 7200,
        "bandwidth": 300000,
        "price": 0,
        "contractStatus": 8,
        "version": 1560,
        "deletedtimestamp": 0,
        "currencyID": "1",
        "renewal-option": 1,
        "contract-start-time": 1461918999000,
        "contract-end-time": 1461926198000
      }],

      "tag": null,
      "connections": ["49b33cd1-5722-4e8e-b40c-03c3407c5efe", "cd354769-d52a-414e-bb7c-f0e0690ddf3b"],
      "type": "0",
      "linkStatus": 6,
      "billing-id": "5f1dd7c5-c48b-44ed-be14-cf2a8730fe27"
    }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<table style="width:100%;text-align:center;">
  <tr>
    <th>contractid</th>
    <th>contractStatus</th> 
    <th>bandwidth</td>
    <th>contract-start-tim</th>
    <th>contract-end-time</th> 
    <th>renewal-option</th>
    <th>price</th> 
    <th></th>
  </tr>
  <tr ng-repeat="contract in customerContracts[0].contracts">
                  <td>{{contract.contractid}}</td>
                  <td>{{contract.contractStatus}}</td>
                  <td>{{contract.bandwidth}}</td>
                  <td>{{contract["contract-start-time"]}}</td>
                          <td>{{contract["contract-end-time"]}}</td>
                  <td>{{contract["renewal-option"]}}</td>
                  <td>{{contract.price}}</td>
                  <td><span class="icon_delete" ng-click="deleteInformation()"></span></td>
  </tr>
</table>
  
</div>

Comments

1

Your JSON is not proper for the ng-repeat because it's an arrat. If you can change the JSON (if it's not coming from a system you are not able to edit) make it like this and your existing html will work. If you know for sure it will not be an array.

$scope.customerContracts =    {
  "description": null,
  "latency": "1",
  "linkid": "26149e91-f6c8-47fb-a10d-691e76486871",
  "contracts": [{
    "contractid": "26149e91-f6c8-47fb-a10d-691e76486871.2",
    "duration": 7200,
    "bandwidth": 300000,
    "price": 0,
    "contractStatus": 8,
    "version": 1560,
    "deletedtimestamp": 0,
    "currencyID": "1",
    "renewal-option": 1,
    "contract-start-time": 1461918999000,
    "contract-end-time": 1461926198000
  }],

  "tag": null,
  "connections": ["49b33cd1-5722-4e8e-b40c-03c3407c5efe", "cd354769-d52a-414e-bb7c-f0e0690ddf3b"],
  "type": "0",
  "linkStatus": 6,
  "billing-id": "5f1dd7c5-c48b-44ed-be14-cf2a8730fe27"
};

If you can not change the JSON format you can do this in your html and it will work:

<div ng-repeat="contracts in customerContracts">
  <table>
    <tr ng-repeat="contract in contracts.contracts">
      <td>{{contract.contractid}}</td>
      <td>{{contract.contractStatus}}</td>
      <td>{{contract.bandwidth}}</td>
      <td>{{contract["contract-start-time"]}}</td>
      <td>{{contract["contract-end-time"]}}</td>
      <td>{{contract["renewal-option"]}}</td>
      <td>{{contract.price}}</td>
      <td><span class="icon_delete" ng-click="deleteInformation()"></span></td>
    </tr>
  </table>
</div>

Comments

1

If you want to use existing json then this will work

<tr ng-repeat="contract in customerContracts[0].contracts">
                  <td>{{contract.contractid}}</td>
                  <td>{{contract.contractStatus}}</td>
                  <td>{{contract.bandwidth}}</td>
                  <td>{{contract["contract-start-time"]}}</td>
                          <td>{{contract["contract-end-time"]}}</td>
                  <td>{{contract["renewal-option"]}}</td>
                  <td>{{contract.price}}</td>
                  <td><span class="icon_delete" ng-click="deleteInformation()"></span></td>
                </tr>

Because customerContracts is an array and you are accessing element from its first value that is a dictionary. So you have to take 0'th element of big array customerContracts[0] now this value customerContracts[0] is a dictionary so you can access from dictionary by simple dot customerContracts[0].contracts

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.