我有 *ngFor 组(其工作),我需要显示“listofGroup”(在控制台中有效,但在 View 中没有),在这个特定组中,我的问题是:如何在 Angular2 中的特定 div 中运行函数
我的 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>
最佳答案
我假设您在 readListsGroup
中设置了属性 listsInGroup
。
它不起作用,因为您正在 div for 循环中调用 readListsGroup
。
这意味着 listsInGroup
具有第一组的值,紧接着它具有第二组的值,依此类推。 div for 循环结束后,listsInGroup
包含最后一个值(最后一个组的值),并且该值绑定(bind)到所有 div。我写了吗?
您可以按照 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>
代码:
readListsGroup(group)
{
const listsInGroup = findit();
return listsInGroup;
}
关于javascript - 如何仅在一个div Angular 2中运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47294384/