如何在Angular4中的click事件中将变量与硬编码值连接起来

时间:2017-08-25 12:02:04

标签: angular

我想在点击事件中添加i的值。 同样的逻辑在*ngif=""和插值标记中工作,但它在(click)=""事件中不起作用。

<div class="popCheckListDropMain" *ngFor="let srkey of srDataList; let i = index">
  <h4 (click)="'accord'+i=!'accord'+i">{{srkey.key}} </h4>
  <ul class="popCheckListDropList" *ngIf="'accord'+i">
    <li *ngFor="let list of srkey.list">
        <div class="customCheckBox">
            <input type="checkbox" id="{{list.name}}" name="{{list.name}}" [(ngModel)]="list.isSelected"
            >
            <div class="check"></div>
        </div>
        <label for="{{list.name}}" >{{list.name}} {{list.isSelected}} </label>
    </li>
  </ul>

1 个答案:

答案 0 :(得分:1)

在你的ts中,添加一个变量来存储每个项目的状态:

toggleAccordions: any = {};

将模板更改为以下内容:

<div class="popCheckListDropMain" *ngFor="let srkey of srDataList; let i = index">
  <h4 (click)="toggleAccordions['accord'+i]=!toggleAccordions['accord'+i]">{{srkey.key}} </h4>
  <ul class="popCheckListDropList" *ngIf="toggleAccordions['accord'+i]">
    <li *ngFor="let list of srkey.list">
        <div class="customCheckBox">
            <input type="checkbox" id="{{list.name}}" name="{{list.name}}" [(ngModel)]="list.isSelected"
            >
            <div class="check"></div>
        </div>
        <label for="{{list.name}}" >{{list.name}} {{list.isSelected}} </label>
    </li>
  </ul>
</div>