Difference between [] and {{}} for binding state to property?

前端 未结 3 607
梦毁少年i
梦毁少年i 2020-12-16 13:08

Here is an template example:



Here b

3条回答
  •  抹茶落季
    2020-12-16 14:07

    Difference between string interpolation and property binding:

    The main thing to understand it the following:

    Interpolation is a special syntax that Angular converts into property binding. It’s a convenient alternative to property binding.

    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):

    • if this value cannot be found there then the value within the string interpolation will be evaluated to a string.
    • If this value is found in the model the value which is found gets coerced to a string and is used.

    This has some implications on how you can use the 2 methods. For example:

    1. String concatenation with string interpolation:

        
      
    2. String interpolation cannot be used for anything else than strings

        
      

    When myInput is an @Input() of myComponent 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 for myInput.

提交回复
热议问题