将函数的返回值传递到自定义Angular组件中

问题描述 投票:-1回答:1

将值传递到这样的自定义组件中有什么问题吗?我注意到,当我在someFunction中使用console.log时,在加载组件时会多次调用它。有人可以解释吗?

HTML

<custom-component [someInput]=“someFunction(‘someParameter’)></custom-component>

TS

someFunction(someParameter) {
    return someValue
}
javascript angular typescript input angular-components
1个回答
0
投票

是的,你是对的,

angular中,您不应呼叫functions中的template

原因

角度的主要目标是,在检测到任何变化时,RenderingDOM。因此,如果角度检测到您的应用程序中有changes/updates,它将重新渲染模板。因此,当每次re-render时,将调用您在template(props)中使用的function

始终遵循最佳做法

why-you-should-never-use-function-calls-in-angular-template-expressions

© www.soinside.com 2019 - 2024. All rights reserved.