1

I want to loop over an object and display every object in a different tag.

For example, if I have this array:

x = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]

I want to display something like this:

first: 1
second: 2

first: 3
second: 4

I tried this:

<ng-container *ngFor="let p of x">
    <div>
        <label for="recipient-a" class="col-form-label">first:</label>
        <input class="form-control" id="recipient-a" type="text" name="a" #name="ngModel" [(ngModel)]="p.a">
    </div>

    <div>
        <label for="recipient-b" class="col-form-label">second:</label>
        <input class="form-control" id="recipient-b" type="text" name="b" #name="ngModel" [(ngModel)]="p.b">
    </div>
</ng-container>

But this will display only the second element:

first: 3
second: 4

first: 3
second: 4

How can I modify the code to achieve what I want? Thank you for your time!

2 Answers 2

1

Please try this

<ng-container *ngFor="let p of x;">
    First: {{p.a}}
    <input class="form-control" type="text" [(ngModel)]="p.a">
    Second: {{p.b}}
    <input class="form-control" type="text" [(ngModel)]="p.b">
</ng-container>
Sign up to request clarification or add additional context in comments.

Comments

1
<ng-container *ngFor="let p of x">
  <div>
    <label for="recipient-a" class="col-form-label">first:{{p.a}}</label>
    <input class="form-control" id="recipient-a" type="text" name="a" #name="ngModel" [(ngModel)]="p.a">
  </div>

  <div>
    <label for="recipient-b" class="col-form-label">second:{{p.b}}</label>
    <input class="form-control" id="recipient-b" type="text" name="b" #name="ngModel" [(ngModel)]="p.b">
  </div>

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.