0

I'm using a ngFor loop to GET a number of elements from a server.


Target: Every element has a View all button, that when is pressed I want to display the entire JSON object that has been generated by that element

Problem: When I press it, I only get the first element.


Code:

<div class="display" *ngFor="let field of fields" >
    <p>Field name: {{field.name}}</p>
    <p>Crop Type: {{field.Crop}}</p>
    <p>Description: {{field.Description}}</p>
    <button class="view" onclick="document.getElementById('ViewAll').style.display = 'block'">View all</button>

  <div  *ngFor="let field of fields" id="ViewAll">
    <p>ID: {{field.ID}}</p>
    <p>Field name: {{field.name}}</p>
    <p>Crop Type: {{field.Crop}}</p>
    <p>Description: {{field.Description}}</p>
    <p>Others: {{field.others}}</p>
  </div>

What is wrong? What have I to change?

3
  • I think on click of viewAll button, you just want to show the name,crop,description in JSON of that particular element ? Commented Jul 17, 2020 at 15:35
  • @DurgeshPal exactly.But if i have more then one object i can't show the second one.Example:Let's say i have the folowing JSON file :[{"name": "Field1","Crop": "CropType","Description": "...","ID": "0"},{"name": "Field2","Crop": "CropType2","Description": "...","ID": "1"}].When i press the button on the second generated element i get the first element from JSON Commented Jul 17, 2020 at 15:38
  • Use *ngIf="visible" in the below div in which you are showing JSON and assign it true on click of view all. Commented Jul 17, 2020 at 15:50

1 Answer 1

3

It's always not recommended to use the document in Angular template. Because mostly Angular will manage the DOM with it's own background processes.

You can achieve this with the help of another boolean variable.

...

<div class="display" *ngFor="let field of fields" >
    <p>Field name: {{field.name}}</p>
    <p>Crop Type: {{field.Crop}}</p>
    <p>Description: {{field.Description}}</p>
    <button class="view" (click)="viewAllBoolean = true">View all</button>
</div>

<ng-container *ngIf="viewAllBoolean">
  <div *ngFor="let field of fields">
    <p>ID: {{field.ID}}</p>
    <p>Field name: {{field.name}}</p>
    <p>Crop Type: {{field.Crop}}</p>
    <p>Description: {{field.Description}}</p>
    <p>Others: {{field.others}}</p>
  </div>
</ng-container>

...

Happy Coding.. :)

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

3 Comments

It is also not recommended to use onclick. You should use (click) instead. By the way, what is <ng-template *ngFor="viewAllBoolean"> supposed to do? I doubt that this is valid template syntax.
Agree and replaced the ng-template tag with ng-container for ngIf to check the boolean flag. (I supposed use ngIf before as well). Thank you for pointed it out. :))
One more observation: I suggest to remove id="ViewAll". There should not be more than one element with a given id, and that id is not used anyway.

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.