Pass Data from Component to Component in Angular

Introduction 
 
This post explains how to pass one component to another component using a decorator. 
 
How to pass data from the Parent Component to the Child component
Angular provides the Decorator, @Input().  By using this decorator, we can pass data from the Parent component to the Child component.
 
How to pass data from the Child component to the Parent Component
Angular provides the Decorator, @Output(). By using this decorator, we can pass data from the child component to the parent component.
 
Adding a Class dynamically
NgClass is a directive where we can set classes dynamically.
 
Below is a screenshot in which there is a sample Form to add values. Added values will display in the card.
 
 
 
I have created Forms and Cards with the help of Bootstrap. In this scenario, I am covering the few topics, including @input(), @Output() and [ngClass] with Form validation.
 
 
  
Step 1: Create an Angular App and Install Bootstrap
 
Create a component called register (ng g c register)
 
There are two types forms Template-Driven (TD) and Reactive Form. I have created a Template-driven form.
 
register.component.html 
  1. <div class="card shadow-lg p-3 mb-5 bg-white rounded">  
  2. <div class="card-header">  
  3. Register Here  
  4. </div>  
  5. <div class="card-body">  
  6. <form (ngSubmit)="SubmitForm(regisForm)" #regisForm="ngForm">  
  7. <div class="form-group row">  
  8. <label for="FirstName" class="col-sm-5 col-form-label">First Name</label>  
  9. <div class="col-sm-7">  
  10. <input type="text"  
  11. ngModel  
  12. name="FirstName"  
  13. #name="ngModel"  
  14. required  
  15. class="form-control form-control-sm">  
  16. <small class="form-text text-muted"  
  17. *ngIf="!name.valid && name.touched">  
  18. First Name is Required</small>  
  19. </div>  
  20. </div>  
  21. <div class="form-group row">  
  22. <label for="LastName" class="col-sm-5 col-form-label">Last Name</label>  
  23. <div class="col-sm-7">  
  24. <input type="text"  
  25. name="LastName"  
  26. ngModel  
  27. #LastName="ngModel"  
  28. required  
  29. class="form-control form-control-sm">  
  30. <small class="form-text text-muted"  
  31. *ngIf="!LastName.valid&&LastName.touched">  
  32. Last Name is Required</small>  
  33. </div>  
  34. </div>  
  35. <div class="form-group row">  
  36. <label for="Mobile" class="col-sm-5 col-form-label">Mobile</label>  
  37. <div class="col-sm-7">  
  38. <input type="number"  
  39. name="Mobile"  
  40. ngModel  
  41. #Mobile="ngModel"  
  42. required  
  43. class="form-control form-control-sm" aria-describedby="emailHelp">  
  44. <small class="form-text text-muted"  
  45. *ngIf="!Mobile.valid && Mobile.touched">  
  46. Mobile Number is Required</small>  
  47. </div>  
  48. </div>  
  49. <div class="form-group row">  
  50. <label for="Location" class="col-sm-5 col-form-label">Location</label>  
  51. <div class="col-sm-7">  
  52. <select class="form-control form-control-sm"  
  53. name="Location"  
  54. ngModel  
  55. #location="ngModel">  
  56. <option *ngFor="let loc of locations" value="{{loc}}">{{loc}}</option>  
  57. </select>  
  58. </div>  
  59. </div>  
  60. <button type="submit" class="btn btn-primary btn-sm" [disabled]="!regisForm.valid">Submit</button>  
  61. <button type="reset" class="btn btn-danger btn-sm ml-2">Reset</button>  
  62. </form>  
  63. </div>  
  64. </div>  
  
register.component.css
 
Below is the class for validation border 
  1. input.ng-invalid.ng-touched{  
  2. border:1px solid red;  
  3. }  
 
register.component.ts
 
Below is an example where I created a variable called tableDataValues with the Output decorator. Whatever we will submit Form will store into tableDataValues.
  1. import { Component, OnInit, Output ,EventEmitter} from '@angular/core';  
  2. import { NgForm } from '@angular/forms';  
  3. @Component({  
  4. selector: 'app-register',  
  5. templateUrl: './register.component.html',  
  6. styleUrls: ['./register.component.css']  
  7. })  
  8. export class RegisterComponent implements OnInit {  
  9. @Output() tableDataValues=new EventEmitter<string>();  
  10. constructor() { }  
  11. ngOnInit() {  
  12. }  
  13. locations = ['Bangalore''Kalaburagi''Solapur'];  
  14. SubmitForm(regisForm:NgForm){  
  15. this.tableDataValues.emit(regisForm.value);  
  16. }  
  17. }  
 
Step 2: Create a component called register-data (ng g c register-data)
 
register-data.component.html
  1. <div class="card shadow p-2 mb-3 rounded mt-1"  
  2. [ngClass]="(item.Location=='Bangalore')?'text-white bg-warning':'text-white bg-success'"  
  3. >  
  4. <div class="card-header">  
  5. Users Account  
  6. <button type="button" class="close" aria-label="Close" (click)="deleteCard(item.Mobile)">  
  7. <span aria-hidden="true">×</span>  
  8. </button>  
  9. </div>  
  10. <div class="card-body">  
  11. <h5 class="card-title">{{item.FirstName | titlecase}}</h5>  
  12. <p class="card-text">  
  13. LastName: {{item.LastName}}  
  14. Mobile: {{item.Mobile}}  
  15. Location:{{item.Location}}  
  16. </p>  
  17. </div>  
  18. </div  
  19. >  
register-data.component.ts
 
Below is example where I created a variable called item with an Input decorator
  1. import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';  
  2. @Component({  
  3. selector: 'app-register-data',  
  4. templateUrl: './register-data.component.html',  
  5. styleUrls: ['./register-data.component.css']  
  6. })  
  7. export class RegisterDataComponent implements OnInit {  
  8. @Input() item;  
  9. @Output() deleteItem = new EventEmitter<string>();  
  10. constructor() { }  
  11. ngOnInit() {  
  12. }  
  13. deleteCard(val) {  
  14. this.deleteItem.emit(val);  
  15. }  
  16. }  
 
NgClass is a directive where we can set its class dynamically.
 
Angular provided many ways to set it dynamically. Below is the small example
We are setting it so that if the selected location is Bangalore, the card will display an orange color, or else displaying a success message.
[ngClass]="(item.Location=='Bangalore')?'text-white bg-warning':'text-white bg-success'"
 
app.component.html
 
The app component is a root component generated by Angular itself. We have added app-register component in app.component.html with the properties tableDataValues. Data will return from the app-register component by submitting values. It will hold and pass to a method that is submitted($event). We also have added an app-register-data component with a properties item.
  1. <div class="container">  
  2. <div class="row">  
  3. <div class="col-md-4">  
  4. <app-register (tableDataValues)="submitted($event)"></app-register>  
  5. </div>  
  6. <div class="col-md-8">  
  7. <div class="row">  
  8. <div class="col-md-4" *ngFor="let items of tableData">  
  9. <app-register-data [item]="items" (deleteItem)="DeletedItem($event)"></app-register-data>  
  10. </div>  
  11. </div>  
  12. </div>  
  13. </div>  
  14. </div>  
app.component.ts
  1. import { Component } from '@angular/core';  
  2. import { NgForm } from '@angular/forms';  
  3. @Component({  
  4. selector: 'app-root',  
  5. templateUrl: './app.component.html',  
  6. styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9. title = 'demo';  
  10. submitted(event){  
  11. this.tableData.push(event);  
  12. }  
  13. DeletedItem(values){  
  14. for(var i = this.tableData.length - 1; i >= 0; i--) {  
  15. if(this.tableData[i].Mobile === values) {  
  16. this.tableData.splice(i, 1);  
  17. }  
  18. }  
  19. }  
  20. tableData=[  
  21. {  
  22. FirstName:"Gururaj",  
  23. LastName:"Jewargi",  
  24. Mobile:1234567890,  
  25. Location:"Bangalore"  
  26. },  
  27. {  
  28. FirstName:"RAju",  
  29. LastName:"Jewargi",  
  30. Mobile:1478523690,  
  31. Location:"Gulbarga"  
  32. },  
  33. {  
  34. FirstName:"Guru",  
  35. LastName:"Jewargi",  
  36. Mobile:9874563215,  
  37. Location:"Kalaburagi"  
  38. }  
  39. ]  
  40. }  
Step 3 : Run the App by ng serve command
 
Fill the required details and click on submit.
 
 
 
If you want to delete a card, click on the close icon.