Angular Component Output Properties

Angular Component Output Properties

In this article, I am going to discuss Angular Component Output Properties in detail. Ā At the end of this article, you will have a very good understanding of the below things.

  1. How to pass the user actions or user entered values or selections from the child component to the parent component usingĀ outputĀ properties.
  2. How to create custom events using angularĀ EventEmitterĀ class
  3. What areĀ ng-containerĀ directive and its use
What are Angular Component Output Properties?

The Angular Component Output Properties are used to to pass the data from the nested component to the container component.

This is a continuation part of our previous article, so please read our previous article before proceeding to this article as we are going to work with the same example as shown below.

Angular Component Output Properties

At the moment when you click any of the radio buttons, then nothing is happening. Let first discuss what we want to do here.

When All(6) radio button is clicked then we need to display all the students in the table. When Male(4) radio button is clicked then we need to display only the 4 Male students in the table similarly when the Female(2) radio button is clicked then we only need to display the 2 Female students in the table.

How to achieve this?

To achieve this here we are going to make use of the Angular componentĀ OutputĀ Properties. First, let’s discuss the changes that are required in the nested component i.e.Ā StudentCountComponent.Ā 

ModifyĀ student-count.component.ts file:

Open student-count.component.ts file and then copy and paste the following code in it. The changes we have done here are self-explained so please go through the comments.

//Ā ImportĀ OutputĀ andĀ EventEmitterĀ fromĀ angular
importĀ {Ā Component,Ā Input,Ā Output,Ā EventEmitterĀ }Ā fromĀ '@angular/core';

@Component({
Ā Ā selector:Ā 'app-student-count',
Ā Ā templateUrl:Ā './student-count.component.html',
Ā Ā styleUrls:Ā ['./student-count.component.css']
})

exportĀ classĀ StudentCountComponentĀ {
Ā Ā @Input()
Ā Ā all:Ā number;

Ā Ā @Input()
Ā Ā male:Ā number;

Ā Ā @Input()
Ā Ā female:Ā number;
Ā Ā //Ā ThisĀ variableĀ holdsĀ theĀ selectedĀ valueĀ ofĀ theĀ radioĀ button
Ā Ā selectedRadioButtonValue:Ā stringĀ =Ā 'All';

Ā Ā //Ā TheĀ OutputĀ decoratorĀ makesĀ theĀ propertyĀ ofĀ anĀ ComponentĀ asĀ anĀ OutputĀ property
Ā Ā //Ā TheĀ EventEmitterĀ classĀ inĀ AngularĀ isĀ usedĀ toĀ createĀ theĀ customĀ event
Ā Ā //Ā WhenĀ theĀ radioĀ buttonĀ selectionĀ changes,Ā theĀ selectedĀ radioĀ button
Ā Ā //Ā valueĀ whichĀ isĀ aĀ stringĀ getsĀ passedĀ toĀ theĀ eventĀ handlerĀ method.
Ā Ā //Ā Hence,Ā theĀ eventĀ payloadĀ isĀ string.
Ā Ā @Output()
Ā Ā countRadioButtonSelectionChanged:Ā EventEmitter<string>Ā =
Ā Ā Ā Ā Ā Ā newĀ EventEmitter<string>();

Ā Ā //Ā ThisĀ methodĀ raisesĀ theĀ customĀ event.Ā WeĀ willĀ bindĀ this
Ā Ā //Ā methodĀ toĀ theĀ changeĀ eventĀ ofĀ allĀ theĀ 3Ā radioĀ buttons
Ā Ā onRadioButtonSelectionChange()Ā {
Ā Ā Ā Ā Ā Ā this.countRadioButtonSelectionChanged
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā .emit(this.selectedRadioButtonValue);
Ā Ā }
}
Modify student-count.component.html

The following are the changes that are required in the view template ofĀ StudentCountComponentĀ i.e. student-count.component.html. Notice here we have made 3 changes on each radio button.

  1. The valueĀ attribute is set to (All, Male or Female)
  2. We implemented the 2-way data-binding using theĀ ngModelĀ directive. NoticeĀ the ngModelĀ is bound toĀ selectedRadioButtonValueĀ property of the component class. This 2-way data-binding ensures whenever the radio button selection changes, theĀ selectedRadioButtonValueĀ property is updated with the value of the selected radio button.
  3. The onRadioButtonSelectionChange()Ā method is binded to theĀ ā€œchangeā€Ā event of the radio button. That means whenever, the selection of the radio button changes,Ā onRadioButtonSelectionChange()Ā method raises the custom eventĀ ā€œcountRadioButtonSelectionChangedā€. We defined this custom event using AngularĀ EventEmitterĀ class.
<spanĀ class="radioClass">ShowĀ :Ā </span>

<inputĀ name='options'Ā type='radio'Ā value="All"
Ā Ā Ā Ā Ā Ā Ā [(ngModel)]="selectedRadioButtonValue"
Ā Ā Ā Ā Ā Ā Ā (change)="onRadioButtonSelectionChange()">
<spanĀ class="radioClass">{{'All('Ā +Ā allĀ +Ā ')'}}</span>

<inputĀ name="options"Ā type="radio"Ā value="Male"
Ā Ā Ā Ā Ā Ā Ā [(ngModel)]="selectedRadioButtonValue"
Ā Ā Ā Ā Ā Ā Ā (change)="onRadioButtonSelectionChange()">
<spanĀ class="radioClass">{{"Male("Ā +Ā maleĀ +Ā ")"}}</span>

<inputĀ name="options"Ā type="radio"Ā value="Female"
Ā Ā Ā Ā Ā Ā Ā [(ngModel)]="selectedRadioButtonValue"
Ā Ā Ā Ā Ā Ā Ā (change)="onRadioButtonSelectionChange()">
<spanĀ class="radioClass">{{"Female("Ā +Ā femaleĀ +Ā ")"}}</span>
Modify student-list.component.ts fileĀ 

Now let’s have a look at the changes that are required in the parent component i.e. StudentListComponent. The following are the changes that are required in theĀ StudentListComponentĀ class. The changes are commentedĀ and self-explained.

importĀ {Ā Component}Ā fromĀ '@angular/core';

@Component({
Ā Ā selector:Ā 'app-student-list',
Ā Ā templateUrl:Ā './student-list.component.html',
Ā Ā styleUrls:Ā ['./student-list.component.css']
})
exportĀ classĀ StudentListComponentĀ {

Ā Ā Ā Ā //Ā ThisĀ propertyĀ willĀ keepĀ trackĀ ofĀ theĀ radioĀ buttonĀ whichĀ isĀ selected
Ā Ā Ā Ā //Ā WeĀ haveĀ setĀ theĀ defaultĀ valueĀ toĀ All,Ā soĀ allĀ theĀ students
Ā Ā Ā Ā //Ā areĀ displayedĀ inĀ theĀ tableĀ byĀ default
Ā Ā Ā Ā selectedStudentCountRadioButton:Ā stringĀ =Ā 'All';

Ā Ā students:Ā any[]Ā =Ā [
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā ID:Ā 'std101',Ā FirstName:Ā 'Pranaya',Ā LastName:Ā 'Rout',
Ā Ā Ā Ā Ā Ā Ā Ā DOB:Ā '12/8/1988',Ā Gender:Ā 'Male',Ā CourseFee:Ā 1234.56
Ā Ā Ā Ā },
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā ID:Ā 'std102',Ā FirstName:Ā 'Anurag',Ā LastName:Ā 'Mohanty',
Ā Ā Ā Ā Ā Ā Ā Ā DOB:Ā '10/14/1989',Ā Gender:Ā 'Male',Ā CourseFee:Ā 6666.00
Ā Ā Ā Ā },
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā ID:Ā 'std103',Ā FirstName:Ā 'Priyanka',Ā LastName:Ā 'Dewangan',
Ā Ā Ā Ā Ā Ā Ā Ā DOB:Ā '7/24/1992',Ā Gender:Ā 'Female',Ā CourseFee:Ā 6543.15
Ā Ā Ā Ā },
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā ID:Ā 'std104',Ā FirstName:Ā 'Hina',Ā LastName:Ā 'Sharma',
Ā Ā Ā Ā Ā Ā Ā Ā DOB:Ā '8/19/1990',Ā Gender:Ā 'Female',Ā CourseFee:Ā 9000.50
Ā Ā Ā Ā },
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā ID:Ā 'std105',Ā FirstName:Ā 'Sambit',Ā LastName:Ā 'Satapathy',
Ā Ā Ā Ā Ā Ā Ā Ā DOB:Ā '4/12/1991',Ā Gender:Ā 'Male',Ā CourseFee:Ā 9876.54
Ā Ā Ā Ā },
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā ID:Ā 'std106',Ā FirstName:Ā 'Tarun',Ā LastName:Ā 'Mallick',
Ā Ā Ā Ā Ā Ā Ā Ā DOB:Ā '4/10/1992',Ā Gender:Ā 'Male',Ā CourseFee:Ā 1278.55
Ā Ā Ā Ā }
];

getTotalStudentsCount():Ā numberĀ {
Ā Ā Ā Ā returnĀ this.students.length;
}

getMaleStudentsCount():Ā numberĀ {
Ā Ā Ā Ā returnĀ this.students.filter(stdĀ =>Ā std.GenderĀ ===Ā 'Male').length;
}

getFemaleStudentsCount():Ā numberĀ {
Ā Ā Ā Ā returnĀ this.students.filter(stdĀ =>Ā std.GenderĀ ===Ā 'Female').length;
}

Ā Ā Ā Ā //Ā DependingĀ onĀ theĀ radioĀ buttonĀ whichĀ isĀ selected,Ā thisĀ methodĀ updates
Ā Ā Ā Ā //Ā theĀ selectedStudentCountRadioButtonĀ property
Ā Ā Ā Ā //Ā ThisĀ methodĀ isĀ goingĀ toĀ calledĀ whenĀ theĀ childĀ componentĀ (StudentCountComponent)
Ā Ā Ā Ā //Ā raisesĀ theĀ customĀ eventĀ -Ā countRadioButtonSelectionChanged
Ā Ā Ā Ā //Ā TheĀ eventĀ bindingĀ isĀ specifiedĀ inĀ StudentList.component.html
Ā Ā Ā Ā onStudentCountRadioButtonChange(selectedRadioButtonValue:Ā string):Ā voidĀ {
Ā Ā Ā Ā Ā Ā Ā Ā this.selectedStudentCountRadioButtonĀ =Ā selectedRadioButtonValue;
Ā Ā Ā Ā }
}
ModifyĀ student-list.component.html

The following are the changes that are required in the view template ofĀ StudentListComponentĀ i.e. student-list.component.html.Ā 

The onStudentCountRadioButtonChange($event)Ā method is bound to the custom event – countRadioButtonSelectionChanged. The $event object will have the selected radio button value as that is what is passed as the event payload from the nested component. The event handler method (onStudentCountRadioButtonChange()) in the component class updates the property ā€œselectedStudentCountRadioButtonā€. This property is then used along with the *ngIf structural directive to decide which student objects to display in the table.

On theĀ <tr>Ā element, we are usingĀ ā€œngIfā€Ā directive along with selectedStudentCountRadioButton property which controls the student objects to display.

Notice, just above the <tr> element, we have introducedĀ <ng-container> element and theĀ ā€œngForā€Ā directive is placed on this element. If you are wondering why we have done this, Angular does not allow multiple structural directives to be placed on one element as shown below.Ā Ā 

<app-student-countĀ [all]="getTotalStudentsCount()"
[male]="getMaleStudentsCount()"
[female]="getFemaleStudentsCount()"
(countRadioButtonSelectionChanged)="onStudentCountRadioButtonChange($event)">
</app-student-count>

<br/><br/>
<table>
Ā Ā Ā Ā <thead>
Ā Ā Ā Ā Ā Ā Ā Ā <tr>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <th>ID</th>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <th>Name</th>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <th>Gender</th>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <th>DOB</th>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <th>CourseĀ Fee</th>
Ā Ā Ā Ā Ā Ā Ā Ā </tr>
Ā Ā Ā Ā </thead>
Ā Ā Ā Ā <tbody>
Ā Ā Ā Ā Ā Ā Ā Ā <ng-containerĀ *ngFor="letĀ studentĀ ofĀ students;">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <trĀ *ngIf="selectedStudentCountRadioButton=='All'Ā ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā selectedStudentCountRadioButton==student.Gender">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <td>{{student.IDĀ |Ā uppercase}}</td>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <td>{{student.FirstNameĀ |Ā uppercase}}</td>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <td>{{student.Gender}}</td>Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <td>{{student.DOBĀ |Ā date:'dd/MM/y'}}</td>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <td>{{student.CourseFeeĀ |Ā currency:'USD':true:'1.2-2'}}</td>
Ā Ā Ā Ā Ā Ā Ā Ā </tr>
Ā Ā Ā Ā </ng-container>
Ā Ā Ā Ā Ā Ā Ā Ā <trĀ *ngIf="!studentsĀ ||Ā students.length==0">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <tdĀ colspan="10">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā NoĀ StudentsĀ toĀ display
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā </td>
Ā Ā Ā Ā Ā Ā Ā Ā </tr>
Ā Ā Ā Ā </tbody>
</table>
Modify app.module.ts:

Open app.module.ts file and then copy and paste the following code in it.

importĀ {Ā BrowserModuleĀ }Ā fromĀ '@angular/platform-browser';
importĀ {Ā NgModuleĀ }Ā fromĀ '@angular/core';
importĀ {Ā FormsModuleĀ }Ā fromĀ '@angular/forms';

importĀ {Ā AppRoutingModuleĀ }Ā fromĀ './app-routing.module';
importĀ {Ā AppComponentĀ }Ā fromĀ './app.component';
importĀ {Ā StudentListComponentĀ }Ā fromĀ './student-list/student-list.component';
importĀ {Ā StudentCountComponentĀ }Ā fromĀ './student-count/student-count.component';

@NgModule({
Ā Ā declarations:Ā [
Ā Ā Ā Ā AppComponent,
Ā Ā Ā Ā StudentListComponent,
Ā Ā Ā Ā StudentCountComponent
Ā Ā ],
Ā Ā imports:Ā [
Ā Ā Ā Ā BrowserModule,
Ā Ā Ā Ā AppRoutingModule,
Ā Ā Ā Ā FormsModuleĀ 
Ā Ā ],
Ā Ā providers:Ā [],
Ā Ā bootstrap:Ā [StudentListComponent]
})
exportĀ classĀ AppModuleĀ {Ā }

That’s it. We have done with our implementation. Now save all the changes and then run the application and you will see based on the selected radio button, the students are displayed in the table.

how to pass the data from the nested component to the container component using the Angular Component Output Properties

In the next article, I am going to discuss Angular Directives in Detail. Here, in this article, I try to explain how to pass the data from the nested component to the container component using the Angular Component Output Properties with an example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

8 thoughts on ā€œAngular Component Output Propertiesā€

  1. Please upload further articles. This website is very useful for learning .net technologies and easy to understand all the topics. Looking for more angular articles

  2. Please upload further articles. This website is very useful for learning .net technologies and easy to understand all the topics. Looking for more angular articles

  3. Your all the articles are great. very clear. I would like to invited you to make wide range of tutorial like this for many other technologies.

  4. after adding ā€˜import { AppRoutingModule } from ā€˜./app-routing.module’;’ getting error ā€˜Can not find the module’.

  5. I am learning from this blog.I found out that here [(ngModel)]=ā€propertyNameā€ is not working. It is not changing values. Instead use [ngModel]=ā€propertyNameā€ (ngModel)=ā€propertyName=$eventā€

Leave a Reply

Your email address will not be published. Required fields are marked *