import {Component, OnInit, ViewChild} from '@angular/core'; import {VehiculeService}…
- User
import {Component, OnInit, ViewChild} from '@angular/core'; import {VehiculeService} from "../services/vehicule.service"; import {ToastrService} from "ngx-toastr"; import {MatSelect} from "@angular/material/select"; import {DriverService} from "../services/driver.service";
@Component({ selector: 'app-vehicle-form', templateUrl: './vehicle-form.component.html', styleUrls: ['./vehicle-form.component.scss'], providers: [], })
export class VehicleFormComponent implements OnInit { submitted: boolean = false; drivers: any; vehicules: any;
@ViewChild('select') select!: MatSelect;
ngOnInit(): void { this.getDrivers(); this.getVehicules(); } constructor(public vehiculeService: VehiculeService, private toast: ToastrService, private driverService: DriverService) {}
getDrivers() { this.driverService.getDrivers().subscribe((data: any) => { this.drivers = data; console.log(data); }); }
getVehicules() { this.vehiculeService.getVehicles().subscribe((data: any) => { this.vehicules = data; console.log(data); }); }
onSubmitVehicule() { this.submitted = true; if (this.vehiculeService.vehicleForm.invalid) { return; } if (this.vehiculeService.vehicleForm.value.Id === 0) { this.insertVehicule(); } else { this.vehiculeService.putVehicle(); } }
private insertVehicule() { this.vehiculeService.postVehicle().subscribe( { next: () => { this.toast.success('Véhicule ajouté avec succès'); this.resetForm(); }, error: () => { this.toast.error('Erreur lors de l'ajout du véhicule'); } } ); }
protected resetForm() { this.submitted = false; this.vehiculeService.vehicleForm.reset(); this.vehiculeService.vehicleForm.setValue({ DateMiseEnCirculation: '', Id: 0, ConsommationMoyenne: 0, DateAchat: '', NumeroAssurance: "", RapportPoidsPuissance: 0, TypeCarburant: "", TypeBoiteVitesse: "", Constructeur: '', Modele: '', NumeroSerie: '', Immatriculation: '', Segment: '', NombrePlaces: 0, Cylindree: 0, Poids: 0, PuissanceMoteur: 0, EmissionCo2: 0, PrixAchat: 0, VignetteCritair: '', ValiditeCritair: '', Achat: true, Location: true, Kilometrage: 0, TypeVehicule: "", DateDerniereModification: '', IdConducteur: 0 }); }
}
- Assistant
- User
<div class="sm:col-span-1"> <div class="mb-2 block"> <label for="model" class="font-medium mb-1">Modèle</label> <select id="model" name="model" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5" formControlName="Modele"> <option *ngFor="let vehicule of vehicules " [value]="vehicule.Modele">{{ vehicule.Modele }}</option> </select> <small class="text-red-600 mt-1" *ngIf="vehiculeService.vehicleForm.get('Modele')?.hasError('required') && (vehiculeService.vehicleForm.get('Modele')?.dirty || vehiculeService.vehicleForm.get('Modele')?.touched)"> Ce champs est requis </small> </div> </div> - Assistant
- User
i want two-bindings Constructeur and Modèle when i select a constructeur in my select list and want automaticaly bind the Modèle list corresponding
- Assistant
- User
i have an error to : TS2345: Argument of type string | null is not assignable to parameter of type to this.models = this.getModelsByConstructor(value); on value
- Assistant
The error you're seeing
TS2345: Argument of type string | null is not assignable to parameter of type...implies that thegetModelsByConstructormethod expects an argument of a certain type (likelystring), but it’s receiving an argument that can be eitherstringornull. SincevaluefromvalueChangescan benullwhen the form control is cleared, you need to either handle the case or ensure that isn't passed to the method in the first place.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.