0

I have a bunch of tasks and they have comments, when I click show comments on a particular list element I want to display only the component for that index.

<ul class="list-group" *ngFor="let item of todayTasks;let i = index">

     <li ..
       <button class="btn btn-info float-right"(click)="displayCommentsComponent(i)">Comments</button>
    </li>
    <app-today-tasks-comments id="{{'tasksComment' + i}}" *ngIf="showCommentComponent"</app-today-tasks-comments> 
</ul>

I don't know what to put in that *ngIf to display one specific element by index, right now it shows all of them.

1 Answer 1

1

Update your code to have the *ngIf dependent on the index: *ngIf="showCommentComponent[i]"

<app-today-tasks-comments id="{{'tasksComment' + i}}" *ngIf="showCommentComponent[i]"></app-today-tasks-comments> 

And then in your class, toggle that index on a map:

showCommentComponent = {};

displayCommentsComponent(index){
  this.showCommentComponent[index] = !this.showCommentComponent[index];
}
Sign up to request clarification or add additional context in comments.

3 Comments

'showCommentComponent: any[] = [];' I made this variable this type and it works but.. Care to explain a bit *ngIf="showCommentComponent[i]" is basically an element right? So I used the debugger and I can see an array of booleans, method inside that switches between true and false, why does that happen when I do statement inside displayComments method? I don't understand why the array is inferred as an array of booleans and just by negation statement with index can switch like this, this is so weird and cool
Yeah that will work too. The reason it does is because this: !this.showCommentComponent[index]; will turn something that is undefined into true.
I've been blessed by one of the vanilla javasript gods haha, this was a very nice lesson to learn thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.