As we know that component is the basic building block of Angular applications, that means that an Angular application is a collection of components and it is possible that one component contains another component for the purpose of reusability. The component which contains another component is called a parent/container component and the component which is contained/present inside another component is called a child/nested component. Then:
- How these components communicate with each other?
- How do we send data from parent component to child component?
- How do we send data from child to parent component?
So, in this article, we will learn how to send data from parent component to the child component. We will build the below screen and will send the data from parent to child component using @Input(). Please note that I have used bootstrap classes to add some basic style.
To build the below screen, we need two components,
- student-list.component.ts (Container/Parent component) - This component is responsible for displaying the student list.
- student-count.component.ts(Nested/Child component) - This component is responsible for displaying the 3 Radio buttons named: All, Male, Female, and we are calling this component inside student-list.component.html using the component’s selector directive.
Now, let’s open student-list.component.ts file and paste the below code.
- import {
- Component
- } from '@angular/core'
- import {
- FormGroup,
- FormControl
- } from '@angular/forms'
- @Component({
- selector: 'my-app',
- templateUrl: './app/student-list.component.html',
- })
- // This is parent(container) component which contains child(nested) Component(student-count.component.ts)
- export class StudentListComponent {
- public students: any; // this variable holds the list of students
- constructor() {
- //Initialized student array to hold some student object
- this.students = [{
- Id: 1,
- Name: "Mahesh",
- Address: "Thane",
- Gender: "Male"
- }, {
- Id: 2,
- Name: "Nishikant",
- Address: "Chembur",
- Gender: "Male"
- }, {
- Id: 3,
- Name: "Sameer",
- Address: "Mulund",
- Gender: "Male"
- }, {
- Id: 4,
- Name: "Nitin",
- Address: "Nahur",
- Gender: "Male"
- }, {
- Id: 4,
- Name: "Siri",
- Address: "Worli",
- Gender: "Female"
- }];
- }
- //Below for simplicity I have created 3 method to get the count of students based on the gender
- //We can do that in single method by passing the parameter.
- //we will use these functions to pass data to the input properties in child component
- getTotalStudentsCount() {
- return this.students.length;
- }
- //Get the maleStudentsCount
- getMaleStudentsCount() {
- return this.students.filter(student => student.Gender == 'Male').length;
- }
- //Get the femaleStudentsCount
- getFemaleStudentsCount() {
- return this.students.filter(student => student.Gender == 'Female').length;
- }
- }
In the above code, I have just created and initialized the "students" array in the constructor and added three methods which will return us the count of the students based on the gender and we are going to use these methods to give values to input properties using property binding.
Now, open student-list.component.html to add some HTML code.

rakshith suvarnaPosted Mar 23, 2018, 1:40 AM
Very nicely explained Thanks!