我使用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>
怎么了?我要改什么?
将字段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>