I have this structor
app/
--pages/
----pages.component.ts
--channel/
----shared/
------assignPlaylist.modal.ts
Inside the pages.component.ts, I have a variable called playlist
export class PagesComponent {
@Input() platform: Platform;
@Output() onPlaylistsChange: EventEmitter<Playlists>;
currentPageName: string;
currentPage: Page;
pages: Array<Page>;
playlists: Playlists;
}
In assignPlaylist.modal.ts, I make an http post method and it returns a new playlists, I need to use that returned playlists to replace the playlists in pages.component.ts
this.apiService.addPlaylistToPage(playlistId, stDate, etDate, this.apiService.getGlobalRegion(), callback)
.subscribe(
data => {
if (this.apiService.g_platform == 'DESKTOP') {
this.apiService.getPlaylist(this.apiService.getCurrentPage(), 'true' )
.subscribe(
res => { this.pagesService.setCurrentPlaylists(res.playlists);
}
);
} else {
this.apiService.getPlaylist(this.apiService.getCurrentPage(), 'false' )
.subscribe(
res => { this.pagesService.setCurrentPlaylists(res.playlists);
}
);
}
}
);
This is the res, return results
And this is the structor of playlists
export class Playlists {
headerPlaylists: Array<Playlist>;
bodyPlaylists: Array<Playlist>;
wholePlaylists: Array<Playlist>;
}
----------------Update---------------------- As mentioned in the answer, I did the following changes.
@Injectable()
export class PagesService {
private currentPlaylists: Subject<Playlists> = new BehaviorSubject<Playlists>(new Playlists());
getCurrentPlaylists() {
return this.currentPlaylists.asObservable();
}
setCurrentPlaylists(playlists: Playlists) {
console.log('i am here');
this.currentPlaylists.next(playlists);
}
}
And the console shows 'I am here',
I write in my pages.component.ts
constructor(private service: PagesService, private playlistService: PlaylistService) {
this.pages = [];
this.currentPage = new Page();
this.service.setCurrentPage(this.currentPage);
this.playlists = new Playlists();
this.onPlaylistsChange = new EventEmitter<Playlists>();
this.service.getCurrentPlaylists().subscribe((playlists) => {
console.log(playlists, 'new playlists coming');
this.playlists = playlists;
}, error => {
console.log(error);
});
}
However, I did not see the message ' new playlists coming', am I using it wrong?
New update: I checked the this.playlists, it's observers is 0, why?