Kyle Abens
Kyle Abens

Reputation: 273

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. Can someone explain?

HTML

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

TS

someFunction(someParameter) {
    return someValue
}

Upvotes: 1

Views: 4346

Answers (1)

BadPiggie
BadPiggie

Reputation: 6359

YES, You are right,

In angular you should not call functions in template.

Reason

The main goal of angular is, Rendering the DOM when detecting any changes. So If angular detect any changes/updates in your application, It will re-render the template. So when It re-render each time, The function you used in template(props) will be called.

Always follow the best practices

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

Upvotes: 3

Related Questions