首页 > 解决方案 > 如何在 html 的另一个组件中使用导出的函数?角度?

问题描述

我在单独的文件中导出了函数

export function sum(population): string {
    return population.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}

我在我的组件中导入这个函数:

import { sum } from  '../../shared/utilities/globalFunction'

以及如何在 html 中使用这个总和?

当我设置

<p> {{ sum(variable-with-data) }}

没有工作...

标签: javascripthtmlangular

解决方案


在 html 中只能在 ts 中使用“变量”public,所以你需要声明一个“变量”(*)并使用一些类似

//in .ts
mysum=sum

//in .html
{{mysum(variable-with-data)}}

就像另一个一样,你不能在 .html 中使用 Math 但你可以使用

//in .ts
myMath=Math

//in .html
{{myMat.cos(3.4)}}

(*) 我说的是变量,但是可以是一个类,一个对象,一个函数......


推荐阅读