简体   繁体   English

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

[英]Passing the return value of a function into a custom Angular component

Is there anything wrong with passing a value into a custom component like this?将值传递给这样的自定义组件有什么问题吗? I noticed that when I console.log inside someFunction it is called many times when the component is loaded.我注意到当我在 someFunction 中使用 console.log 时,它会在加载组件时被多次调用。 Can someone explain?有人可以解释一下吗?

HTML HTML

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

TS TS

someFunction(someParameter) {
    return someValue
}

YES, You are right,你是对的,

In angular you should not call functions in template .angular不应template调用functions

Reason原因

The main goal of angular is, Rendering the DOM when detecting any changes. angular 的主要目标是,在检测到任何更改时Rendering DOM So If angular detect any changes/updates in your application, It will re-render the template.因此,如果 angular 检测到您的应用程序中有任何changes/updates ,它将重新渲染模板。 So when It re-render each time, The function you used in template(props) will be called.所以当它每次重新渲染时,你在template(props) 中使用的函数都会被调用。

Always follow the best practices始终遵循最佳实践

why-you-should-never-use-function-calls-in-angular-template-expressions 为什么你永远不应该使用角度模板表达式中的函数调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM