Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a string value to a component in angular2

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:

<component [inputField]="string"></component>

Unfortunately, only expressions are allowed on the right side of the assignment. Is there a way to do this?

like image 769
Andras Hatvani Avatar asked Mar 25 '16 12:03

Andras Hatvani


People also ask

How do I pass a string to a component?

Interaction between components can be done using two decorators. 1. @Input decorator: A string can be passed from a parent component to a child component using @Input decorator in class and a directive property of component decorator.


Video Answer


3 Answers

String literals can be passed in different ways:

<component inputField="string"></component>
<component [inputField]="'string'"></component>
<component inputField="{{'string'}}"></component>
like image 99
Günter Zöchbauer Avatar answered Oct 19 '22 13:10

Günter Zöchbauer


You can pass a string by enclosing the string in quotes

<component [inputField]="'string'"></component>
like image 53
Eric Martinez Avatar answered Oct 19 '22 12:10

Eric Martinez


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.

like image 3
Glenn Avatar answered Oct 19 '22 11:10

Glenn