Sunil Yadav
Sunil Yadav

Reputation: 94

How to update object value on click using behaviorsubject Angular 5?

Service.ts: I have create an object in my service.ts file, Now I want to update object values using BehaviorSubject, so I can use this updated object in my different components

private _menuUpdate = {
  bankFlag: false,
  contactsFlag: false,
  educationDataFlag: false,
  examFlag: false,
  existingRelationShipFlag: false,
  nomineeFlag: false,
  personalFlag: false,
  supportDocsFalg: false,
  workExpFlag: false
}

 public menuDisableStatus = new BehaviorSubject<Object>(this._menuUpdate);
 menuStatus = this.menuDisableStatus.asObservable();

changeStatus(statusObj) {
  this.menuDisableStatus.next(statusObj);
}

Now in my components1.ts Example

    click(){
      this.menuDisableStatus.educationDataFlag = true;          
      this.mliService.changeStatus(this.menuDisableStatus.educationDataFlag);          
    }

Now in my components2.ts Example

 this.mliService.menuStatus.subscribe((data) => {         
    this.menuDisableStatus = data;       
  });
 console.log(this.menuDisableStatus);

Here console give me 'true' instead of Update object.

Upvotes: 0

Views: 686

Answers (1)

Torsten Weggen
Torsten Weggen

Reputation: 151

In your component1 you call changeStatus (and in result the "next" of the observable) not to the primary subject but to a property of this subject.

Change it to this.mliService.changeStatus(this.menuDisableStatus); and it should work

Upvotes: 1

Related Questions