I have a shared service SharedService
@Injectable()
export class SharedService {
private title = new BehaviorSubject<string>("");
currentMessage = this.title.asObservable();
constructor() { }
setData(val: string) {
this.title.next(val);
}
}
I have a component, where I get new data
export class Fill implements OnInit {
public title;
constructor(public shared: SharedService) {
}
ngOnInit() {
this.shared.setData(this.title);
}}
And the component Info, where I want read new data export class InfoComponent implements OnInit {
constructor(public shared: SharedService) {
this.title = ''; }
ngOnInit() {
console.log('i am title from info ')
this.shared.currentMessage.subscribe(title => this.title = title)
console.log(this.shared.currentMessage);
console.log(this.title);
}}
In both cases of console.log I get Objects, that contains information, that I need - but I can't retrieve value of it. So, on my console it look like
But if try something like
console.log(this.shared.currentMessage.source.source.value);
it says Property 'source' is protected and only accessible within class 'Observable' and its subclasses.
this.shared.currentMessage.value
this.title.value
doesn't work also... How could I retrieve data (value) of one or another?
ngOnInit
is nested into constructor insideInfoComponent
. - Egor Stambakio