javascript - 如何在 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) }}
没有工作...
解决方案
在 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)}}
(*) 我说的是变量,但是可以是一个类,一个对象,一个函数......
推荐阅读
- algorithm - 动态分离图表的 y 坐标
- python - 如何使用python从以列中的数字开头的字符串中提取数字
- jupyter-notebook - 在 Jupyter 中,你如何判断你在哪个浏览器中?
- reactjs - Stack Navigation Using React Native 导致 undefined is not object 错误
- php - 取数组值并与其余部分分开
- html - Divs 并排坐成正方形
- qt - QQuickWidget:grabToImage:项目的窗口不可见
- python - 从 Singleton 接收 pyqtSignal
- flutter - Flutter - 当文本字段具有焦点时隐藏提示文本
- python - 如何将列表的数量乘以列表中先前零的数量?