0

hello I m making an app in angular 2 ,I have a component named test-summary.component and another component report.component , while running my app I have two components on two tabs, I want to pass an updated data from test-summary.component to report.component ,that data should be reflected to report.component in real time..I have tried a lot using subject,SubjectBehavior but I could not..Plz some one can help..?

Asif Khan
  • 159
  • 1
  • 1
  • 13

3 Answers3

0

Use event emitor for cross component to transfer data between components

//test-summary.component import { Events } from 'ionic-angular'; constructor(public events: Events) {} dataupdate(user) {this.events.publish('realtimereport', realtimedata);} //report.component constructor(public events: Events) { events.subscribe('realtimereport', (realtimedata) => { this.chartData = realtimedata;console.log(realtimedata);});}

you can use this from anywhere in you project

Raja Mohamed
  • 1,026
  • 9
  • 22
0

One Solution could be using services to hold the common data.

Both components can reference the same service variable.

If one component updates the variable, the changes would be automatically reflected in the other component.

rootkill
  • 599
  • 5
  • 10
0

What you do is use an @Input and a @Output (https://angular.io/guide/component-interaction). The parent page that show the tabs can be your communication.

In your test-summary.component:

export class Summary{
@Output() sumbitReport = new EventEmitter<any>();

someMethod(){
this.sumbitReport.emit(yourReport); 

In your report.component:

export class Report{
@Input() report: any;

Then in your parent component:

tabMethod(){
this.summaryReportTab= true;

parentDoSomethingWithReport(report: any){ // get from summary
this.parentReport = report; // pass to report component

And in your parent template:

<summary *ngIf="summaryReportTab" (sumbitReport)="parentDoSomethingWithReport($event)">></summary>
<report *ngIf="reportTab" [report]="parentReport"></report>
Swoox
  • 3,707
  • 2
  • 21
  • 32
  • I dont want to use @input output it will render the report component.html which I dnt want , I just want to pass updated data to the report.component in real time – Asif Khan Aug 25 '17 at 06:42
  • In Summary localStorage.setItem('report', yourReport); In Report this.report = localStorage.getItem('report'); and localStorage.removeItem('report'); – Swoox Aug 25 '17 at 06:53
  • localStorage takes string as a second parameter, I want to send an object – Asif Khan Aug 25 '17 at 06:55
  • Save your value in the browser. So even if you refresh before you go to report component your report still exist until you remove it. https://developer.mozilla.org/en/docs/Web/API/Window/localStorage \(same thing for chrome) – Swoox Aug 25 '17 at 06:56
  • It can only handle string but you can but JSON in with localStorage.setItem('report', JSON.stringify(yourReport)); and this.report = JSON.parse(localStorage.getItem('report')); – Swoox Aug 25 '17 at 07:03
  • it will work if both the components are on the same browser , but in reality report component can be on another machine another browser – Asif Khan Aug 25 '17 at 07:11
  • Then you have to make a service with an call and a backend. Angular is web-based not server. – Swoox Aug 25 '17 at 07:13
  • Try and read https://stackoverflow.com/questions/34516651/set-base-url-for-angular-2-http-requests It's outside of the basic will be really hard to support you. Good luck – Swoox Aug 25 '17 at 07:18