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.
- app.component.ts
import { Component } from '@angular/core' ;
@Component({
selector: 'example' ,
templateUrl: './app.component.html' ,
})
export class AppComponent {
}
|
- app.component.html
< example name = "GeeksForGeeks1" ></ example >
< example [name]="'GeeksForGeeks2'"></ example >
< example name = " {{'GeeksForGeeks3'}} " ></ example >
|
- Output:
GeeksForGeeks1
GeeksForGeeks2
GeeksForGeeks3
Similar Reads
How to concat strings using AngularJS ?
In this article, we will see how to concat strings in AngularJS. There are few ways to concat the strings in AngularJS. In this article, we will see 2 of them. Example 1: In the first example, we are using the '+' operator to concat the strings [GFGTABS] HTML <!DOCTYPE HTML> <html> <h
2 min read
How to convert string into a number using AngularJS ?
In this article, we will see how to convert a string into a number in AngularJS, along with understanding its implementation through the illustrations. Approach: The parseInt() method is used for converting the string to an integer. We will check whether the string is an integer or not by the isNumb
2 min read
Angular PrimeNG Form Password Styling Component
Angular PrimeNG is an open-source front-end UI framework developed by PrimeTek for developing efficient and scalable angular applications. Using PrimeNG in their projects helps developers to cut down the development time and focus on other important areas of the application. In this article, we will
4 min read
Angular PrimeNG Form Password Templating 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 Form Password Templating Component in Angular PrimeNG.
3 min read
How to Concatenate Pipe Values in a String in Angular?
Pipes in Angular are features that let us transform the data into some other format in the templates. For example, converting a number into a currency, or a value into a date, etc. In this article, we will learn how to concatenate pipe value in a string with the help of a code example. Step 1: Initi
2 min read
Angular PrimeNG Form InputMask Styling Component
Angular PrimeNG is a collection of Angular UI components. It is an open-source library developed by PrimeTek. It has many ready-to-use components that make it one of the top choices of angular developers when it comes to choosing a UI library. In this article, we will see the Angular PrimeNG Form In
4 min read
How to create a new component in Angular?
A component in Angular is the building block for making web pages. It is a reusable block of code that can be used anywhere in the app any number of times. It provides scalability, reusability, and readability. Each component does a specific job like showing a menu, a photo or a card, etc. In this a
3 min read
Angular PrimeNG Form Password Templates Component
Angular PrimeNG is an open-source front-end UI library that has many native Angular UI components which help developers to build a fast and scalable web solution. This article will discuss the Angular PrimeNG Form Password Templates Component. The Password Component is used to take the input of sens
3 min read
Angular PrimeNG tag 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 know how to use the tag component in Angular PrimeNG. tag component: It is
3 min read
Angular PrimeNG Form ToggleButton Labels 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 know how to use the ToggleButton Labels Component in Angular PrimeNG. The T
3 min read