One component is sending values to another:
paquete-detalle.component.ts
nombre: string;
precio: number;
ngOnInit() {
const id = this.activatedRoute.snapshot.paramMap.get('id');
this.fs.getPaqueteObject(id).valueChanges().subscribe(paquete => {
this.nombre = paquete.nombre;
this.precio = paquete.precio;
});
}
paquete-detalle.component.html
{{nombre}} {{precio}} //Values are printed correctly here.
<app-reserva [precio]="precio" [nombre]="nombre"></app-reserva>
Next, in reserva.component.ts:
@Input() precio: number;
@Input() nombre: string;
constructor( private fs: FirebaseService, private fb: FormBuilder, ) {
this.forma = fb.group ({
nombre: [ this.nombre ],
precio: [ this.precio ],
});
}
ngOnInit() {
console.log(this.precio, this.nombre);
}
While trying to save the values in a reactive form, I am getting null
(undefined
in the console) for this.price
and this.name
.
However, in reserva.component.html, the values are displayed as expected:
<div class="text-center">
<h4>{{nombre}}</h4>
<p>From: USD {{precio}} / person</p>
</div>
What is the correct method to pass values between components?