I have a service, to which I would like to pass a string from any component in my app.
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class TestService {
  private messageSource = new BehaviorSubject<string>('');
  currentMessage = this.messageSource.asObservable();
  constructor() {}
  changeMessage(message) {
    this.messageSource.next(message);
  
  }
}
in my child component, I update it:
  constructor(private ts: TestService) {}
  onTestMe() {
    this.ts.changeMessage('foobar');
  }
in another (in this case, parent) component, I subscribe to it:
import { Component, OnInit } from '@angular/core';
import { TestService } from './test.service';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  message: string;
  constructor(private ts: TestService) {}
  ngOnInit(): void {
    this.ts.currentMessage.subscribe((message) => (this.message = message));
    console.log('message on root page: ', this.message);
  }
}
in the root HTML, I have a binding {{message}}
The binding is updating but how do I update the value of the message variable in the TS whenever it's changed?
In other words, I need to know the value of message to use in other functions in the TS and I need to be sure it's current.
See stackblitz of above: https://stackblitz.com/edit/angular-ivy-6u7ueb?file=src/app/hello.component.html
 
     
     
    