Say, I have a component which is used as following:
<health-renderer
[health]="getHealth()"
[label]="label">
<health-renderer>
After reading data-binding related parts from https://angular.io/guide/template-syntax, it seem like the way I am setting target component property health
is wrong as the template expression used is getHealth()
which is a method. And method binding should only be done with events, not properties. In other words, template expression (the thing on right-hand side of =
) need to be a template variable, template reference variable or a component/directive/element property.
If [target]="methodCall()"
is a wrong way of doing binding, then why is it allowed by Angular? If this is the right way of doing binding then is my understanding given in last paragraph wrong?
Also, how should I then modify my code to reflect the right thing for:
- Showing current health which is say, just a progress bar
- Automatically call
getHealth(): integer
which contains business logic for calculating health. 0 will show nothing on progress bar for health. 100 will fill up the progress bar.
Lastly, I noticed getHealth() getting called like 10-20 times for no reason on each mouse move or click. May be binding a method to a target property is not a good practice because of this change-detection behavior of Angular?