Search code examples
angularrxjsobservablebehaviorsubject

Angular typescript observable with get/set


I want to get and set data from observable but I want to have possibility to subscribe too.

Can I do something like this or is there better approach?

public $someName: BehaviorSubject<string> = new BehaviorSubject<string>(null);

    set someName(value: string) {
        if (this.$someName.value != value) {
            this.$someName.next(value);
        }
    }

    get someName() {
        return this.$someName.value;
    }

Now I can access $someName observable and someName get/set. So is there some better approach?


Solution

  • create a another getter like this

    get sameNameChange(){
    
      return this.$someName.asObservable();
    
    }