Reputation: 1646
I have a layout page (component) in app module where I display a title text. I have another lazy loaded module where I have button on click of which I am changing the text value.
I provided service in app.module only and injected in layout component as well as the component of lazy loaded module. On button click, I am updating the value of societyName of the service in component of lazy loaded module.
I tried below approach:
import { Subject } from 'rxjs/Subject';
@Injectable()
export class DataSharingService {
private societyName = new Subject<string>();
societyNameObservable$ = this.societyName.asObservable();
updateSocietyName(societyName: string) {
this.societyName.next(societyName);
}
}
Subscription is not happening in layout page.
this.dataSharingService.societyNameObservable$.subscribe(value=>{
console.log(value);
})
So, far I found solution for non-lazy loaded modules. If anyone have any idea,let me know. Thanks in advance.
I want to switch the society Name on click of switch button. Switch page is part of lazy loaded module and Keerthi Gardenia text is coming from layout page which is part of app module. Currently I am achieving this by doing page reload which is not a best option.
Upvotes: 0
Views: 1540
Reputation: 3848
Does this work for you.?
export class DataSharingService {
private societyName = new Subject<string>();
updatedSocietyValue() {
return this.societyName.asObservable();
}
updateSocietyName(societyName: string) {
this.societyName.next(societyName);
}
}
Layout page
this.dataSharingService.updatedSocietyValue().subscribe(value=>{
console.log(value);
})
Upvotes: 1