4

How do I use a foreach statment in angular js? My current data is printing out as a json format. I want to make it printed out on new lines.

Html

<p>{{controller.greeting.custinfo}}</p>

 //attempt
<p ng-repeat= >{{controller.greeting.custinfo}}</p>

Output on UI

{"id":null,"biling_address":"null","billing_state":"FL","ip_Addresses":["123:111:2101","","NULL"],name":"jimmmy"}

java output

System.out.println(custinfo);
demo.model.cert@ba1983a

How can I use a foreach statement to display the data in new lines? Rather than a json format.

I know how to do this with thymeleaf,example below

<table th:each="custinfo : ${custinfo}">

  <tr>
        <td ng-show="id" class="padded">id:</td>
        <td ng-show="id" th:text="${custinfo.id}" class="padded"></td>
  </tr>
  //continue down

4 Answers 4

7

Just loop thru it:

 <p ng-repeat="(key, value) in controller.greeting.custinfo">{{key}}: {{value}}</p>
Sign up to request clarification or add additional context in comments.

6 Comments

In my case what would be key/value?
On first row: key = "id" & value = "null".
Ok, key would work, but not value. Value changes everytime. So I can't just set the value
If your object changes, the key/value (output) will change too. Check E2E binding of AngularJS.
Got it, you gave me the answer. Will accept when times up.
|
3

To display a list using AngularJS, you need an array that you will iterate.

Let's say you have list of customers

$scope.customers = [{id:1, name:'customer1'}, .....];

You will do so this

 <ul>
<li ng-repeat="custInfo in customers">{{custInfo.name}}       </li>
</ul>

Comments

2

AngularJS can iterate over object with ng-repeat.

$scope.data = {"id":null,"biling_address":"null","billing_state":"FL","ip_Addresses":["123:111:2101","","NULL"],"name":"jimmmy"};

<table>
  <tr ng-repeat="(key, value) in data">
    <td>{{key}}</td>
    <td class="padded">{{value}}</td>
  </tr>
</table>

Example for your data: https://jsfiddle.net/fn8rqjz2/1/

If you want to show or hide elements based on id:

<tr ng-show="data.id" ng-repeat="(key, value) in data">

Comments

0

Try this:

<p ng-repeat="(key, value) in yourObj">{{yourObj[key]}}</p>

If I understood what are you trying to do.

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.