简体   繁体   中英

How to run function only in one div Angular 2

I have *ngFor with groups (its working), i need show "listofGroup" (in console works but in view no), in this specific group, my question is: how to run function in specific div in Angular2
my html groups

<div *ngFor="group in groups">
  <h4>{{group.name}}</h4>
  //here I want to call function where shows list in this group
  //I wrote this but not working
  <ul (load)="readListsGroup(group._id)">
    <li *ngFor="let listGroup of listsInGroup">
      <span>{{listGroup.name}}</span>
    </li>
  </ul>
</div>

I am assuming that you set property listsInGroup in readListsGroup .
It is not working, because you are calling readListsGroup in div for loop. That means that listsInGroup have value of first group, and immediate after that it have value of second group and so on. After div for loop is ended, listsInGroup contains last value (value of last group) and this value is bind to all divs. Am I write?

You can rewrite template as user184994 write:

<div *ngFor="group in groups">
  <h4>{{group.name}}</h4>
  //here I want to call function where shows list in this group
  //I wrote this but not working
  <ul>
    <li *ngFor="let listGroup of readListsGroup(group)">
      <span>{{listGroup.name}}</span>
    </li>
  </ul>
</div>

Code:

readListsGroup(group)
{
  const listsInGroup = findit();
  return listsInGroup;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM