This is source code
<div>
{{ getActualData() }}
</div>
<div>
{{ getVirtualData() }}
</div>
<div>
{{ getActualData() - getVirtualData() }}
</div>
This is what I want.
<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?
Choose Template > New Variable from the editor toolbar (or choose an existing variable to add it to the page) Enter a name for the variable. Press Enter (by default this will create a single-line text input field)
To get started using template reference variables, simply create a new Angular component or visit an existing one. To create a template reference variable, locate the HTML element that you want to reference and then tag it like so: #myVarName .
You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a context object whose properties can be assigned to variables using let binding syntax. Note that you must specify an outlet for the template, but it can be a reference to itself.
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
you can try :
<div *ngIf="getActualData(); let actual">
<div *ngIf="getVirtualData(); let virtual">
{{ actual - virtual }}
</div>
</div>
</div>
this is a workaround but should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With