gpt4 book ai didi

javascript - 单击按钮后隐藏元素 - angular2 - ngFor

转载 作者:行者123 更新时间:2023-11-29 21:05:16 24 4
gpt4 key购买 nike

我仍在学习 Angular2,目前找不到解决我问题的方法。我正在使用 ngFor 显示 1-3 层/包,看起来像这样 tiers in display mode .单击编辑按钮后,我只希望单击的元素进入编辑模式,目前所有元素都切换到编辑模式,看起来像这样 tiers in edit mode .

这是代码,将其简化为相关部分。

<h1 class="page-title"><span class="fw-semi-bold">Packages</span></h1>
<div class="row">
<div class="col-md-4" *ngFor="let tier of tiers; let i=index;">
<!-- Show Mode -->
<ul [hidden]="editMode" class="pricing-table">
<li class="plan-name">
{{ tier.title }} {{i}}
</li>
<li>
description
</li>
<li>
description
</li>
<li>
description
</li>
<li>
description
</li>
<p class="plan-price">{{ tier.price }}</p>
<li class="plan-action">
<a class="btn btn-primary" (click)="this.onEdit()">Edit</a>
<a class="btn btn-success" (click)="this.clickSave()">SaveYes</a>
<a class="btn btn-warning" >Get</a>
</li>
</ul>
<!-- Edit Mode -->
<ul [hidden]="!editMode" class="pricing-table-edit">
<li class="plan-name-edit">
<input type="text" class="form-control" style=" font-size: 3rem;" placeholder="{{ tier.title }} " formControlName="package_name">

</li>
<li >
<div class="rewards-edit" style="background-color: #3a3d46; ">Feature 1</div>
</li>
<li>
<div class="rewards-edit" style="background-color: #3a3d46">Feature 1</div>
</li>
<li>
<div class="rewards-edit" style="background-color: #3a3d46">Feature 1</div>
</li>
<li>
<div class="rewards-edit" style="background-color: #3a3d46">Feature 1</div>
</li>
<div style="align:center; padding-left: 40%; padding-right: 40%;"><input type="text" id="package_price1" class="form-control plan-price" placeholder="4,99" formControlName="package_price"></div>

<li class="plan-action">
<a class="btn btn-primary">Edit</a>
<a class="btn btn-success">Save</a>
<a class="btn btn-warning">Publish</a>
</li>
</ul>
</div>

组件文件:

export class PackagesComponent {
editMode = false;

onEdit () {
this.editMode = true;
}
}

有没有办法在我的方法中使用索引变量,以便我知道当前点击了 3 层中的哪一层?或者还有其他解决方案或想法吗?

非常感谢:)

更新代码,感谢 Abrar:

export class PackagesComponent {  //this has to be renamed to TierComponent for less confusion
public tierToEdit: Tier;
editMode = false;
tiers: Tier[] = [
new Tier(
10,
'First Tier',
[
{id: 500, title: 'desc', description: 'crazy description'},
{id: 501, title: 'desc', description: 'crazy description'},
{id: 502, title: 'desc', description: 'crazy description'},
{id: 503, title: 'desc', description: 'crazy description'}
],
5.00),
*[more code..]*
]


constructor (private tierService: TierService) {}

ngOnInit() {
}



onEdit (tier: Tier) {
this.editMode = true;
this.tierToEdit = tier;
}

模板文件:

<h1 class="page-title"><span class="fw-semi-bold">Packages</span></h1>
<div class="row">
<div class="col-md-4" *ngFor="let tier of tiers; let i=index;">
<!-- Show Mode -->
<ul *ngIf="!editMode || tierToEdit != tier" class="pricing-table">
<li class="plan-name">
{{ tier.title }} {{i}}
</li>
<li class="plan-name">
{{ tier.title }} {{i}}
</li>
<li>
description
</li>
<li>
description
</li>
<li>
description
</li>
<li>
description
</li>
<p class="plan-price">{{ tier.price }}</p>
<li class="plan-action">
<a class="btn btn-primary" (click)="onEdit(tier)">Edit</a>
<a class="btn btn-success" (click)="this.clickSave()">SaveYes</a>
<a class="btn btn-warning" >Get</a>
</li>
</ul>
<!-- Edit Mode -->
<ul *ngIf="editMode && tierToEdit == tier " class="pricing-table-edit">
<li class="plan-name-edit">
<input type="text" class="form-control" style=" font-size: 3rem;" placeholder="{{ tier.title }} " formControlName="package_name">

</li>
<li >
<div class="rewards-edit" style="background-color: #3a3d46; ">Feature 1</div>
</li>
<li>
<div class="rewards-edit" style="background-color: #3a3d46">Feature 1</div>
</li>
<li>
<div class="rewards-edit" style="background-color: #3a3d46">Feature 1</div>
</li>
<li>
<div class="rewards-edit" style="background-color: #3a3d46">Feature 1</div>
</li>
<div style="align:center; padding-left: 40%; padding-right: 40%;"><input type="text" id="package_price1" class="form-control plan-price" placeholder="4,99" formControlName="package_price"></div>

<li class="plan-action">
<a class="btn btn-primary">Edit</a>
<a class="btn btn-success">Save</a>
<a class="btn btn-warning">Publish</a>
</li>
</ul>
</div>

最佳答案

我会做的是,当您单击以编辑其中一个层时,将层传递到编辑函数中。

然后设置一个变量,如 selectedTier 以设置为该层或与该层关联的某种类型的 id/名称。

然后您可以将该 selectedTier 与您的 ngFor 中的层匹配。

然后要隐藏除选定的以外的所有内容,请使用 [hidden]="selectedTier != tier"

关于javascript - 单击按钮后隐藏元素 - angular2 - ngFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44422531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com