1

I want to add accord with value of i inside click event. my same logic is working in *ngif="" and interpolation mark but it is not working in (click)="" event.

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

7
  • What are you trying to achieve? 'accord'+i =!... is not a vliad expression Commented Aug 25, 2017 at 12:06
  • Sorry if I read this wrong but what you want to accomplish here? If you put 'accord+i' onClick() your *ngIf() will be always true? Commented Aug 25, 2017 at 12:06
  • I guess he's trying to toggle a accord547 boolean Commented Aug 25, 2017 at 12:07
  • @Mozgor seen it. Commented Aug 25, 2017 at 12:14
  • @Swoox my comment trying to explain his expected behavior wasn't an example of clarity either :) Commented Aug 25, 2017 at 12:16

1 Answer 1

1

In your ts, add a variable to store every item's state:

toggleAccordions: any = {};

Change you template to following:

<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>
Sign up to request clarification or add additional context in comments.

6 Comments

Was writting kinda the same. Ugly as hell and not what I'd call 'best practise', but an easy way to fix OP's problem.
By the way, for a toggled content, *ngIf should not be used. Pick [hidden]="whatever_condition" instead
thanks a lot its working but I also want to know clean and proper way to do this.
@RajnishRajput this is a clean way, you have to store state of each accordion, otherwise you just have same state for all of them. Please upvote and mark accepted if it helped :)
@Mozgor *ngIf is recommended over [hidden]. Read here: angularjs.blogspot.de/2016/04/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.