How to pass string to a component using Angular2 ?
Last Updated :
15 Apr, 2020
In angular2, a component is considered as a
'view' with its own logic and data. Data binding is the process of combining data values with their corresponding representations. For components, string values can be passed, either directly as string literals or by storing the string literal in a variable. There are 3 ways of passing string values to components in angular2. String value bindings are described in the template section of the component.
Passing a string value to a component from its class: Here, the message variable will get its value either from the constructor of the class or by using angular event bindings(user inputs).
Syntax:
<component>{{message}}</component>
Example: It is the simplest way of passing a string to a component. The component 'AppComponent' has a selector called 'app-display' and the template for display is directed to 'app.component.html'. When the class of the component contains the string data, it can be accessed by the component by interpolation of the 'content' variable.
2.Passing of string value between nested components: Here, the
inputField is the attribute and message is the variable that holds the required string. Interaction between components can be done using two decorators.
- @Input()
- @Output()
Syntax:
<component [inputField]="message"></component>
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. When a variable in child component is declared with @Input decorator, that variable can be 'received' by the child component from the parent component template.
Example: The container component(parent) should pass the value of 'send' to the nested component(child). Using property binding, the binding target 'message' gets content from the binding source 'send'. The directive property of component decorator for the parent component specifies that the
'childComponent' needs to be used. At the receiver end, the child has the @Input decorator which receives the string from parent and uses it accordingly.
2. @Output decorator: This approach is used to emit data from the child, which is received by the parent. For an @Output decorator to decorate any property of the nested class, the property type must be an event. The only way a nested component can pass back data to its container is with an event. The data to pass is called event payload. In angular2, an event is defined with an EventEmitter object.
Example: The child component will have an instance of an EventEmitter object using @Output property. A function is invoked (here, a button click) to trigger the passing of string. On the other hand, the parent component which has the child component bounded to it using the directives decorator will receive the payload using another function that can be used as per interest.
Note:The above two methods are used for passing strings to components by interpolation.
Passing the string value directly to the component without interpolation: Here, 'message' itself is the string and is given as input to the inputField attribute of the component.
Syntax:
<component inputField="message"></component>
(or)
<component [inputField]="'message'"></component>
(or)
<component inputField="{{'message'}}"></component>
Example: The following example covers all the three methods.
Similar Reads
How to pass a string to a custom pipes constructor - Angular 16 In Angular, custom pipes are a powerful tool for transforming data in templates. While simple pipes perform basic transformations, there are cases where you may need to pass parameters to customize the behavior of a pipe. Fortunately, Angular provides mechanisms for passing parameters to custom pipe
3 min read
How To Get The URL Parameters Using AngularJS? In AngularJS, retrieving URL parameters is essential for managing dynamic content and user interactions based on the URL state. In this article, we'll learn various approaches to achieve this.Table of ContentApproach 1: Using ActivatedRoute ServiceApproach 2: Using Router ServiceApproach 3: Using UR
3 min read
How to inject service in angular 6 component ? Service is a special class in Angular that is primarily used for inter-component communication. It is a class having a narrow & well-defined purpose that should perform a specific task. The function, any value, or any feature which may application required, are encompassed by the Service. In oth
4 min read
Angular PrimeNG Form Inputtext Icons Component Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn how to use the InputText Icons Component in Angular PrimeNG. Letâs le
3 min read
How to set default values for Angular 2 component properties? In Angular 2, we can pass the value of a variable between two components (from parent to child) using the Input Decorator. Using these Input decorators we can also set the default values of the properties. Below I had detailly elaborated in a comprehensive way on how to set default values for Angula
2 min read
Components in Angular 8 The component is the basic building block of Angular. It has a selector, template, style, and other properties, and it specifies the metadata required to process the component. Creating a Component in Angular 8: To create a component in any angular application, follow the below steps: Get to the ang
2 min read
Angular PrimeNG Form Inputtext Sizes Component Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn how to use the InputText Sizes Component in Angular PrimeNG. Letâs le
3 min read
Introduction To Components And Templates in Angular Angular is a powerful framework for building dynamic, single-page web applications. One of the core concepts that make Angular so effective is its use of components and templates. Components and templates are the building blocks of any Angular application, allowing developers to create reusable, mai
6 min read
Angular PrimeNG Form InputSwitch Properties Component Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Form InputSwitch Properties Component in Angular PrimeNG. T
3 min read
Angular PrimeNG Form Chips Custom Content Component Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the Angular PrimeNG Form Chips Custom Content Component. The Ch
3 min read