【问题标题】:Passing the return value of a function into a custom Angular component将函数的返回值传递给自定义 Angular 组件
【发布时间】:2020-05-12 01:58:47
【问题描述】:

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

HTML

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

TS

someFunction(someParameter) {
    return someValue
}

【问题讨论】:

    标签: javascript angular typescript input angular-components


    【解决方案1】:

    是的,你是对的,

    angular 中,您不应该template 中调用functions

    原因

    angular 的主要目标是,RenderingDOM 在检测到任何变化时。因此,如果 Angular 在您的应用程序中检测到任何changes/updates,它将重新渲染模板。所以当它每次重新渲染时,你在template(props)中使用的function都会被调用。

    始终遵循最佳做法

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

    【讨论】:

    • 祝你好运!
    • 这是一个从 Angular 开始的常见错误。 +1 易于消化且直截了当的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2011-07-05
    • 2012-12-06
    相关资源
    最近更新 更多