在我的角2模板,我在的形式极其繁琐的计算:Angular 2:将数学语句分配给模板中的变量?
<td class="text-right"><strong>
{{((avgGrossProfitPer * addMonthlySalesVolume) + (addMonthlySalesVolume * addCarryingCostProf/100) +
(accessAmount + 10 + numChecks + (addMonthlySalesVolume * 0.075)) || '0') | currency:'USD':true}}
</strong>
</td>
这里所有的变量进行input [(ngModel)]="templateName"
设置。但是,我想要做的是将此计算的值设置为一个变量,因此我不必再次复制/粘贴整个公式。
我已经试过是:
<td class="text-right"><strong #monthlyroi>
{{((avgGrossProfitPer * addMonthlySalesVolume) + (addMonthlySalesVolume * addCarryingCostProf/100) +
(accessAmount + 10 + numChecks + (addMonthlySalesVolume * 0.075)) || '0') | currency:'USD':true}}
</strong>
</td>
<!--Try to return the value of the calculation above-->
<span>{{monthlyroi}}</span>
然而,{{monthlyroi}}
返回 “[HTML元素的对象]” 在网页上呈现的时候。
你有没有考虑只是把一个函数接受你所需要的参数和返回你想要的值双花括号? – Gab
我有,虽然我是新来Angular 2,所以不知道什么是最好的方法来处理这个。我试着在'app.component中写一个函数。ts'接受这些值作为参数,但是我希望每次用户更改输入时都自动更新这个值,所以我在模板中到处都有很多'(keyup)=“functionName(myparamaters)''让事情变得笨重。 – user3183717
相反,您可以使用'(change)= ...'而不是'(keyup)= ...' – Gab