html - How do I get a button used on multiple card containers to only affect one container at a time?
问题描述
I am currently working on a web application using angular and angular material. The page in question holds several profiles each with a button that when clicked removes the information currently showing and replaces it with the rest of the profile information. The issue is that when I click this button on one profile it does this information flip on all the profiles at once. I need help figuring out how to have the button only affect the profile it is associated with.
I am still relatively new to coding, so I've mainly been doing research on advice on how to approach this particular situation, but nothing I've found so far has worked.
<mat-card class="bpBuddyCont" *ngFor= "let profile of profileList">
<div>
<ul class="bpBuddies">
<li class="li2">
<div *ngIf="!showHiddenInfo">
<mat-card-title class="specifics">{{profile.dogName}}</mat-card-title>
<mat-card-subtitle class="subtitle">Owner ~ {{profile.ownerFirstName}}</mat-card-subtitle>
<mat-card-subtitle>Member Since {{profile.yearJoined}}</mat-card-subtitle>
</div>
<div class="column4" *ngIf="showHiddenInfo">
<span class="details4">Additional Notes: </span>
<span class="details3">{{profile.additionalNotes}}</span>
</div>
</div>
<button mat-button color="primary" class="btn" (click)="showHiddenInfo = !showHiddenInfo">{{showHiddenInfo ? 'Back' : 'More...'}}</button>
</li>
</ul>
</div>
</mat-card>
解决方案
您可以为每个对象使用一个布尔变量来执行此操作,profile.isExpanded
单击时将其更改为:
(click)="profile.isExpanded = !profile.isExpanded"
HTML 代码:
<mat-card class="bpBuddyCont" *ngFor="let profile of profileList">
<div>
<ul class="bpBuddies">
<li class="li2">
<div *ngIf="!profile.isExpanded">
<mat-card-title class="specifics">{{profile.dogName}}</mat-card-title>
<mat-card-subtitle class="subtitle">Owner ~ {{profile.ownerFirstName}}</mat-card-subtitle>
<mat-card-subtitle>Member Since {{profile.yearJoined}}</mat-card-subtitle>
</div>
<div class="column4" *ngIf="profile.isExpanded">
<span class="details4">Additional Notes: </span>
<span class="details3">{{profile.additionalNotes}}</span>
</div>
<button mat-button color="primary" class="btn" (click)="profile.isExpanded = !profile.isExpanded">{{profile.isExpanded ? 'Back' : 'More...'}}</button>
</li>
</ul>
</div>
</mat-card>
TS 文件没有变化。
推荐阅读
- css - 如何在一个之后显示元素
,在屏幕内? - php - Laravel:如何从 API 获取 json 响应
- r - 在R中将浮点数转换为字符串而不会丢失精度
- angular - Stackblitz:如何使用 Angular 执行测试?
- uwp-xaml - XAML 错误但不是
- active-directory - LDAP 无效凭据 - Active Directory Windows Server 2012 R2
- r - 通过另一个数据帧的两列中的值对两个数据帧中的数据帧进行子集
- matrix - Xcode simd - 平移和旋转矩阵示例的问题
- javascript - d3js v5 + topojson v3 在后台访问对象的属性
- cassandra - 如何在 Cassandra 中设计一个包含事件日期和四列过滤的表格