import { Component , OnInit } from '@angular/core' ; import { Vehicle } from '../../model/vehicle' ; import { VehicleService } from '../../services/vehicle.service' ; @ Component ( { selector : 'app-vehicle-list' , templateUrl : './vehicles.component.html' , styleUrls : [ './vehicles.component.css' ] } ) export class VehiclesComponent implements OnInit { public selectedVehicle : Vehicle ; public vehicles : Vehicle [ ] ; constructor ( private service ) { } ngOnInit ( ) { this . selectVehicle ( new Vehicle ( ) ) ; this . loadVehicles ( ) ; } public selectVehicle ( vehicle ) { this . selectedVehicle = Object . assign ( { } , vehicle ) ; } public clearSelectedVehicle ( ) { this . selectVehicle ( new Vehicle ( ) ) ; } public loadVehicles ( event ? ) { this . service . getVehicles ( ) . subscribe ( data => { this . vehicles = data . vehicles ; } ) ; } public addVehicle ( vehicle ) { this . vehicles . push ( vehicle ) ; } public updateVehicle ( vehicle ) { const index = this . vehicles . findIndex ( v => v . id === vehicle . id ) ; if ( index > - 1 ) { this . vehicles [ index ] = vehicle ; } } public removeVehicle ( vehicleId ) { this . vehicles = this . vehicles . filter ( v => v . id !== vehicleId ) ; } }