[英]Create local variable in Angular template
I'm using Angular 6. 我正在使用Angular 6。
I would like to assign a local variable in for loop to avoid multiple call of the method. 我想在for循环中分配一个局部变量,以避免多次调用该方法。
<li *ngFor="let item of items">
<!-- Declare someItem variable here -->
<!-- let someItem = getSomeItemFromDifferentDataStructureUsingSomeLogic(item) -->
{{ someItem.name }}
{{ someItem.anotherValue }}
</li>
I don't also want to make a separate component to display someItem
cause it is too trivial to create separate one. 我也不想创建一个单独的组件来显示someItem
因为创建单独的组件太简单了。
What would be your's approach? 您的处理方式是什么?
Many thanks for a help. 非常感谢您的帮助。
I would map
items to someItem
s in compontent code and interate over it 我会map
项目someItem
了它在compontent代码和interate小号
In compontent: 补充:
someItems=items.map(this.getSomeItemFromDifferentDataStructureUsingSomeLogic.bind(this))
; someItems=items.map(this.getSomeItemFromDifferentDataStructureUsingSomeLogic.bind(this))
;
and in template 并在模板中
<li *ngFor="let someItem of someItems">
{{ someItem.name }}
{{ someItem.anotherValue }}
</li>
In case if you need in this *for
loop original item, just create an array of item-someItem pairs and iterate over that, using what is needed respectively. 如果您在此*for
循环中需要原始项目,只需创建一个item-someItem对数组,然后分别使用所需的对象对其进行迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.