如何在Angular模板中声明变量? [英] How to declare variable in template of Angular?

查看:111
本文介绍了如何在Angular模板中声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是源代码

<div>
{{ getActualData() }}
</div>
<div>
{{ getVirtualData() }}
</div>
<div>
 {{ getActualData() - getVirtualData() }}
</div>

这就是我想要的.

<div>
{{ actual = getActualData() }}
</div>
<div>
{{ virtual = getVirtualData() }}
</div>
<div>
{{ actual - virtual }}
</div>

因为两个函数很复杂,所以我想暂时保存数据并很快计算出差值. 有什么办法吗?

Because two functions are complex, I would like to save data temporarily and calculate difference shortly. Is there any way to do this?

推荐答案

您可以使用 let 在模板中声明变量,该变量将评估函数并获取结果,并使用ngIf实际检查是否值在那里,并分配给变量

You can declare variable in template using let that will evaluate the function and get the result , using ngIf to actually check if the value is there and assign to the variable

<div *ngIf="getActualData(); let actual" > 
<div *ngIf="getVirtualData(); let virtual" > 
 {{actual - virtual}}
</div>

演示

DEMO

这篇关于如何在Angular模板中声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆