I am developing an angular application, where I am trying to pass a value from a parent component to a child component using the @Input() decorator. however, the value still renders the old/initial value from the child component and not the updated/most recently passed value from the parent component. Does anyone understand why and how to fix this?
Code:
@Input('amt') amount:any =100;
parent component
<app-payment [amount]="100*2"></app-payment>
2
Answers
Yeah, it turned out that i the decorator name and the variable name has to be matching, i dont know why but this fixed the little issue.
change:
To
and that should work, if you find yourself making a similar mistake
For
@Input
decorator we have optional argumentbindingPropertyName
which controls how we can bind that property in html, if no supplied it defaults to the name of property.so, if you write
You are setting
bindingPropertyName
toamt
hence to use it in template you need to write something like.you can skip
bindingPropertyName
and it will use default value (name of property hereamount
) or use something else if needed.Generally, we don’t use
bindingPropertyName
but sometimes for directives we may want to avoid conflict of binding property name (as we can apply a directive to a component which may also define a property with same name andbindingPropertyName
) so we can prefix it with directive name.