Angular 2: Difference between property binding with and without brackets?

后端 未结 5 512
失恋的感觉
失恋的感觉 2020-12-16 10:39

I noticed it\'s possible to property bind stuff without brackets. What is the difference?

Typescript:

import { Component, Input } from \'@angular/cor         


        
相关标签:
5条回答
  • 2020-12-16 11:20

    From Docs - Remember the brackets:

    The brackets, [], tell Angular to evaluate the template expression. If you omit the brackets, Angular treats the string as a constant and initializes the target property with that string.

    0 讨论(0)
  • 2020-12-16 11:27

    Here is brief description of all situation:

    When you use square brackets, the right hand side is an expression. When you use no brackets at all, the right hand side is a constant.

    So will assign 'constant' string to my-tag's input. To achieve the same effect with square brackets: notice the single quote inside. If you didn't put single quote there, my-tag's input will be tied to its parent component's (where you write this template) property or template variable (like ngFor's let-constant for example) named "constant".

    form here What is the difference between property binding and just a regular attribute

    0 讨论(0)
  • 2020-12-16 11:28

    In general, we can say that we should use bindings without brackets only when we have a fixed string property:

    From the docs:

    You should omit the brackets when all of the following are true:

    1. The target property accepts a string value.
    2. The string is a fixed value that you can bake into the template.
    3. This initial value never changes.

    You routinely initialize attributes this way in standard HTML, and it works just as well for directive and component property initialization.

    When setting an element property to a non-string data value, you must use property binding.

    All in all, that means that the value on the right only gets interpreted when using brackets. You can remove the brackets whenever you see quotes in quotes on the right: [anyStringProperty]="'hello'" can be changed to anyStringProperty = "hello".

    So, property binding without square brackets is possible as long as we also omit the single quotation marks between the double quotation marks when passing down a string.

    0 讨论(0)
  • 2020-12-16 11:30

    There are some Cases where we need to add like this html attributes dynamically might be and example which comes json from api request

    Case 1 [] known as Property Binding

    <my-comp [foo]="data.bar"></my-comp>
    

    Case 2 {{ }} known as Interpolation

    <my-comp foo="{{data.bar}}"></my-comp>
    

    Case 3 Conditional handling

    <my-comp [attr.foo]="data.bar ? true : false"></my-comp>
    

    Both {{ }} called as Interpolation and [] called as Property Binding means angular understand that there is One-way from data source to view target.

    For more visit www.codementor.io

    0 讨论(0)
  • 2020-12-16 11:32

    There is a small, maybe unimportant situation, but in some cases it can be annoying that you miss it.

    Number


    Case 1

    <my-comp [foo]="90"></my-comp>
    

    Case 2

    <my-comp foo="90"></my-comp>
    

    Case 1: typeof foo => 'number'

    Case 2: typeof foo => 'string'

    Boolean


    Case 3

    <my-comp [foo]="true"></my-comp>
    

    Case 4

    <my-comp foo="true"></my-comp>
    

    Case 3: typeof foo => 'boolean'

    Case 4: typeof foo => 'string'

    0 讨论(0)
提交回复
热议问题