6

I created a behaviour subject in a service class.

public personObject: BehaviorSubject<any> =
    new BehaviorSubject<any>({ personId: 1, name: 'john doe' });

On a component that imports this service, i subscribed this behaviour subject like this:

this._subscription.add(
    this._bankService.personObject.subscribe(data => {
        this.personObject = data;
        console.log(data);
    })
);

But I am not able to get exact dataset in the behaviour subject.

Edit I forgot to mention that I used ViewContainerRef to create my sibling component which I added to an answer with a few comments.

Prabesh
  • 353
  • 1
  • 6
  • 20
  • Can you make a demo? – martin Oct 12 '17 at 09:51
  • Add more info (code) about the service and component classes – Jota.Toledo Oct 12 '17 at 09:51
  • To make it simple, I just want to pass an object between sibling components using BehaviorSubject. Just any demo or article that provides information on how to do it will be helpful. Thanks – Prabesh Oct 12 '17 at 09:57
  • SO is not a "how to" forum, you come here with a concrete problem and ppl help you. If you cant give us a minimal reproduction of your problem, its hard for anyone to give you advice – Jota.Toledo Oct 12 '17 at 10:58

3 Answers3

13

Service

@Injectable()
export class DataService {

  private _dataListSource: BehaviorSubject<IData[]> = new BehaviorSubject([]);
  dataList: Observable<IData[]> = this._dataListSource.asObservable().distinctUntilChanged();

  getDataList(): Observable<any> {
      return this.httpService.get('/data').map(res => {
          this._dataListSource.next(res);
      });
  }
}

TS file

export class DataComponent implements OnInit {

    public dataList$: Observable<IData[]>;

    constructor(public dataService: DataService) {}

    ngOnInit() {
        this.dataList$ = this.dataService.dataList;
        this.dataService.getDataList().subscribe();
    }
}

HTML file

<div *ngIf="dataList$ | async; let dataList; ">
    <div *ngFor="let data of dataList">
        {{ data | json}}
    </div>
</div>
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
1

I forgot to mention that I was using I was using ViewContainerRef to create a sibling component and it turns out behavior subject does not work the same way with component created using ViewContainerRef.

Other wise Behaviour subjects of any object work exactly like with number or string. I used @Input to send data to component for now.

Prabesh
  • 353
  • 1
  • 6
  • 20
1

(Sender) In the component where you change object properties:

import { StoreService } from '../../services/store.service';

constructor(private _storeService: StoreService) {}

ngOnInit(): void {
    let person = { personId: 0, name: '' };
    this._storeService.personObject.subscribe( value => {
      person.personId = value.personId; // set value personId from service
    } );
    this._storeService.order.next({...person, name: 'somebody'  } ); // set new value for name property
  }

(Receptor) In the component where you subscribe to personObject:

import { StoreService } from '../../services/store.service'; // service file with personObject variable

constructor(private _storeService: StoreService) {}

this._storeService.personObject.subscribe( value => {
  console.log(value);
} );
Diego Venâncio
  • 5,698
  • 2
  • 49
  • 68