How to pass a string value to a component in angular2

前端 未结 3 992
清歌不尽
清歌不尽 2020-12-04 13:55

I\'d like to pass a string value to a component in angular2, but it doesn\'t work with the default binding. I\'m thinking of something similar to this:



        
相关标签:
3条回答
  • 2020-12-04 14:44

    You can pass a string by enclosing the string in quotes

    <component [inputField]="'string'"></component>
    
    0 讨论(0)
  • 2020-12-04 14:44

    To include a single quote (and possibly other special HTML characters) in the string literal the first option works while those that use single quotes to wrap the literal fail with parse errors. For example:

    <component inputField="John&#39;s Value"></component>
    

    Will output "John's Value" correctly.

    0 讨论(0)
  • 2020-12-04 14:54

    String literals can be passed in different ways:

    <component inputField="string"></component>
    <component [inputField]="'string'"></component>
    <component inputField="{{'string'}}"></component>
    
    0 讨论(0)
提交回复
热议问题