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
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
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 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 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 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
How to add background-image using ngStyle in Angular2 ?
The ngStyle Attribute is used to add some style to an HTML element. In this article, we will learn How to add background-image using ngStyle in Angular. To achieve this, we will follow the below approaches.Table of ContentSteps for Installing & Configuring the Angular ApplicationProject Structur
3 min read
Angular PrimeNG Form SelectButton Single Selection 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 Form SelectButton Single Selection Component in Angular
3 min read
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
Angular PrimeNG Terminal 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 Terminal component in Angular PrimeNG. We will also lea
3 min read