Im fairly new to Angular and am having trouble passing an array to a different (not parent/child related) component. What I want in my app is to click on a button to mark it as 'accepted', and to display that same array in a different view (the accepted arrays view) , in a different route. I've tried doing it with @Input and with shared Services and it just wont work. Can you please point me in the right direction? Thank you.
sharedService.ts //My array is called 'Turno'
import {Injectable} from '@angular/core';
import {Turno} from '../models/turno';
@Injectable()
export class SharedService{
public turno:Turno;
constructor(){
}
setArray(turno){
this.turno=turno;
}
getArray(){
return this.turno;
}
}
first component (I mark an accepted array with the accept() method):
@Component({
selector: 'app-turnos',
templateUrl: './turnos.component.html',
styleUrls: ['./turnos.component.css'],
providers:[TurnoService, SharedService]
})
export class TurnosComponent implements OnInit {
public turnos: Turno;
public status;
constructor(
private _turnoService: TurnoService,
private _sharedService: SharedService
) { }
ngOnInit(): void {
this._turnoService.getTurnos().subscribe(
response=>{
if(response.status== 'success'){
this.turnos= response.turnos;
this.status=response.status;
console.log(this.turnos);
}else{
this.status= 'error';
}
},error=>{
console.log('error');
}
);
}
accept(turno){
this._sharedService.setArray(turno);
console.log(turno);
}
second component (receives and lists accepted arrays)
import { Component, OnInit} from '@angular/core';
import {SharedService} from '../../services/shared.service';
import {Turno} from '../../models/turno';
@Component({
selector: 'app-turnoaceptado',
templateUrl: './turnoaceptado.component.html',
styleUrls: ['./turnoaceptado.component.css'],
providers:[SharedService]
})
export class AcceptedTurnoComponent implements OnInit {
public turnos: Turno;
constructor(
private _sharedService: SharedService
) { }
ngOnInit(): void {
this.turnos=this._sharedService.getArray();
console.log(this.turnos);
}
}