Here is an template example:
<span count="{{currentCount}}"></span>
<span [count]="currentCount"></span>
Here both of them does the same thing. Which one is preferred and why?
Here is an template example:
<span count="{{currentCount}}"></span>
<span [count]="currentCount"></span>
Here both of them does the same thing. Which one is preferred and why?
[]
is for binding from a value in the parent component to an@Input()
in the child component. It allows to pass objects.{{}}
is for binding strings in properties and HTML likewhere the binding can be part of a string.
()
is for binding an event handler to be called when a DOM event is fired or anEventEmitter
on the child component emits an eventPlunker example
More details in https://angular.io/docs/ts/latest/guide/template-syntax.html#!#binding-syntax
Difference between string interpolation and property binding:
The main thing to understand it the following:
This implies that under the hood it yields a similar outcome. However, string interpolation has one important limitation. This is that everything within string interpolation will first be evaluated (trying to find a value from the model ts file):
This has some implications on how you can use the 2 methods. For example:
String concatenation with string interpolation:
String interpolation cannot be used for anything else than strings
When
myInput
is an@Input()
ofmyComponent
and we want to pass in an object, we have to use property binding. If we were to use string interpolation the object would be turned into a string and this would be passed in as a value formyInput
.