Search code examples
angularangular2-directivesangular2-databinding

What's the advantage/disadvantage of writing `[defaultColor]="'violet'"` in Angular 2


In angular.io Docs Advanced you can read following code:

<p [myHighlight]="color" [defaultColor]="'violet'">
  Highlight me too!
</p>

I thought that when binding to a constant value, it should be sufficient (and more efficient?) to write

defaultColor="violet"

Am I wrong? Why would one dynamically bind to a constant value?


Solution

  • If defaultColor is an @Input() it's more obvious for the reader of the code because defaultColor="violet" could be just an old-style HTML attribute.

    Besides that, there is no disadvantage of using the one or the other.

    defaultColor="violet" is actually added to the DOM while [defaultColor]="'violet'" is only visible in the components template because this will be replaced by JavaScript code when the components template is compiled.

    If this is an advantage or disadvantage depends on what you try to accomplish.