I suggest that best approach is that to use rxjs subject. You can pass data between modal dialog component or whatever it is. Create new rxjs subject in your service like this
import { Subject } from 'rxjs/Subject';
@Injectable()
export class MyService {
myNewSubject = new Subject<any>();
informDataChanges(passyourobject){
this.myNewSubject.next(passyourobject);
}
}
When change happens in your component or you want to pass data to another component simply call this service function from your component and pass data as parameter to this function. You can do that with something like this
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.css']
})
export class SomeComponent implements OnInit {
constructor( private myService: MyService) { }
someFunction(){
this.myService.informLogout('somedata');//Passing data to service here
}
ngOnInit() {
}
}
Now all you need to do is subscribe to that data in another component. IMPORTANT subject keeps on watches any changes to it, and data will be continuous stream and it will be automatically subscribed. So it is best to subscribe to the subject in the constructor, and changes will be immediately reflected in that component.
You do it with some thing like this
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-another',
templateUrl: './another.component.html',
styleUrls: ['./another.component.css']
})
export class AnotherComponent implements OnInit {
constructor( private myService: MyService) {
this.myService.myNewSubject.subscribe(data=>{
console.log(data);
})
}
This way you can pass data between any components easily.