提问者:小点点

选择特定元素


我使用ngfor循环从服务器获取多个元素,每个元素都有一个“View All”按钮,当按下该按钮时,显示生成该元素的整个JSON对象,但当按下该按钮时,只获取第一个元素。

<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>

怎么了?我要改什么?


共1个答案

匿名用户

将字段id添加到id中,以便您具有唯一的id

null

<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{{field.ID}}').style.display = 'block'">View all</button>

  <div  *ngFor="let field of fields" id="ViewAll{{field.ID}}">
    <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>
  
</div>