Hi there we have prepared a simple program for passing value to parent component to child component.
what this Angular program does, step by step. Imagine we're building a simple webpage with two main parts: a "Parent" section where you can type something, and a "Child" section that displays what you typed. Let's check step by step.
What the Program Does
This program demonstrates how data is shared between two parts of an Angular application, called "components." Specifically, it shows how data moves from a "Parent" component to a "Child" component. The user types text into an input box in the Parent Component, and that text is then displayed in the Child Component.
Key Concepts
- Components: Angular applications are built using components. You can think of a component as a reusable building block for your webpage. Each component has its own HTML structure (what it looks like) and its own logic (how it behaves). In our case, we have a ParentComponent and a ChildComponent.
- Parent and Child: Components can be organized in a tree-like structure. One component can contain (or "use") another component. The component that contains the other is called the "parent," and the contained component is called the "child." Here, ParentComponent contains ChildComponent.
ngModel: This is a special directive in Angular that helps us work with form inputs (like text boxes). It does two things:- It displays the current value of a variable in the input box.
- It updates that variable whenever the user types something in the input box.
- Data Flow: In this program, data flows in one direction: from the ParentComponent to the ChildComponent. The ParentComponent has a variable that holds the text the user types. It then sends that variable's value to the ChildComponent, which displays it.
@Input(): To allow a Child Component to receive data from its Parent, we use@Input(). In the ChildComponent,@Input() parentInputValue: string;declares that the ChildComponent expects to receive a string of data named "parentInputValue".
How the Program Works (Step-by-Step)
- User Interaction (Parent):
- The user sees an input box in the ParentComponent.
- When the user types something, the
ngModeldirective updates theinputValuevariable in the ParentComponent with whatever the user types.
- Passing Data (Parent to Child):
In the ParentComponent's HTML,
<app-child [parentInputValue]="inputValue"></app-child>is how the data is passed.[parentInputValue]tells Angular to bind theparentInputValueproperty of the ChildComponent.="inputValue"specifies that the value of theinputValuevariable from the ParentComponent should be passed to theparentInputValueproperty of the ChildComponent.- Receiving Data (Child):
- The ChildComponent has a property called
parentInputValue, decorated with@Input(). This means it's designed to receive a value from its parent. - When the ParentComponent passes the data, Angular automatically updates the
parentInputValueproperty in the ChildComponent.
Displaying Data (Child):
- The ChildComponent then displays the value of
parentInputValuein its HTML, so the user sees the text they typed in the ParentComponent.
0 Comments