Home > database >  Angular - get data over service between components
Angular - get data over service between components

Time:07-19

I am pretty new with Angular and I will exchange data over service. In the view of the receiving component I have the following html:

<app-timeconfirmation [institution] = "timeconfirmationafterchangeService.getInstitution()" [scheduleIntervalContainers] = "timeconfirmationafterchangeService.getScheduleContainers()"></app-timeconfirmation>

Is there a possibility in the receiving component to get the data not in der view but in the *.js component?

CodePudding user response:

You can create a service and injected into those 2 components. something like:

data.service.ts

@Injectable({
  providedIn: 'root'
})
export class DataService {

  data: BehaviorSubject<any> = new BehaviorSubject<any>(null);
}

component-one.component.ts

@Component({
    ....
})
export class ComponentOneComponent {
    constructor(private dataService: DataService) {
        dataService.data.next(...);
    }
}

component-two.component.ts

@Component({
    ....
})
export class ComponentTwoComponent {
    constructor(private dataService: DataService) {
        dataService.data.subscribe(...);
    }
}
  • Related