简体   繁体   English

如何仅在一格Angular 2中运行功能

[英]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 我有* ngFor组(它的工作),我需要显示“ listofGroup”(在控制台工作,但在视图中没有),在这个特定的组中,我的问题是:如何在Angular2的特定div中运行功能
my html groups 我的html组

<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 . 我假设您在readListsGroup设置属性listsInGroup
It is not working, because you are calling readListsGroup in div for loop. 它不起作用,因为要在div中调用readListsGroup进行循环。 That means that listsInGroup have value of first group, and immediate after that it have value of second group and so on. 这意味着listsInGroup具有第一组的值,紧随其后具有第二组的值,依此类推。 After div for loop is ended, listsInGroup contains last value (value of last group) and this value is bind to all divs. div for循环结束后, listsInGroup包含最后一个值(最后一个组的值),并且此值绑定到所有div。 Am I write? 我在写吗?

You can rewrite template as user184994 write: 您可以在user184994写入时重写模板:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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