I have the following parent component:
<h1>Vehicle Inventory</h1>
<p *ngIf="!vehicles"><em>No vehicle entries found</em></p>
<div *ngIf="vehicles">
<ul class="nav nav-pills nav-fill">
<li class="nav-item">
<a class="nav-link" routerLink="/home/price" routerLinkActive="active">Search By price</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/home/make-model" routerLinkActive="active">Search By Make Or Model</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/home/engine-capacity" routerLinkActive="active">Search By Engine Capacity</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/home/cylinder-variant" routerLinkActive="active">Search By Cylinder Variant</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/home/cylinder-capacity" routerLinkActive="active">Search By Cylinder Capacity</a>
</li>
</ul>
<router-outlet></router-outlet>
</div>
<table class="table" *ngIf="vehicles">
<thead class="thead-dark">
<tr>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Engine Capacity</th>
<th scope="col">Cylinder Variant</th>
<th scope="col">Top Speed</th>
<th scope="col">Price (R)</th>
<th scope="col">Cylinder Capacity</th>
<th scope="col">Air Pressure/second</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let vehicle of vehicles">
<td>{{ vehicle.Make }}</td>
<td>{{ vehicle.Model }}</td>
<td>{{ vehicle.EngineCapacity }}</td>
<td>{{ vehicle.CylinderVariant }}</td>
<td>{{ vehicle.TopSpeed }}</td>
<td>{{ vehicle.Price }}</td>
<td>{{ vehicle.IndividualCylinderCapacity }}</td>
<td>{{ vehicle.AirPressurePerSecond }}</td>
</tr>
</tbody>
</table>
What can be viewed from the above is, I have some navigation going on here that will determine the child component being loaded into the <router-outlet>
.
My child component emits an event via EventEmitter
, as can be seen below:
import { Component, OnInit, Input, Output } from '@angular/core';
import { VehicleService } from '../../Services/VehicleService';
import { EventEmitter } from '@angular/core';
import { Vehicle } from '../../Models/Vehicle';
@Component({
selector: 'app-search-cylinder-capacity',
templateUrl: './search-cylinder-capacity.component.html',
styleUrls: ['./search-cylinder-capacity.component.css']
})
export class SearchCylinderCapacityComponent implements OnInit {
@Input() cylinderCapacity: any;
@Output() dataNotifier: EventEmitter<Vehicle[]> = new EventEmitter();
constructor(private service: VehicleService) { }
ngOnInit() {
}
searchVehicle() {
this.service
.SearchVehiclesByCylinderCapacity(this.cylinderCapacity)
.subscribe(response => this.dataNotifier.emit(response));
}
}
How do I capture this event's response, so that my parent component's vehicle: Vehicle[]
can be populated with the response of the event?