Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe on variable changes?

Tags:

I have declared the following variable:

public filter: IFilterWeekScheduleClassShort = {};

How to subscribe on changes(listen object filter)?

I tried this:

private filter: IFilterWeekScheduleClassShort = {};
private filterChanges: BehaviorSubject<IFilterWeekScheduleClassShort> = new BehaviorSubject(this.filter);

this.filterChanges.subscribe((model) => {
    console.log(model);
});

Is it okay?

My final code:

public _filter: IFilterWeekScheduleClassShort = {};
private filterChanges: BehaviorSubject<IFilterWeekScheduleClassShort> = new BehaviorSubject(this._filter);

  this.filter = {a: 1, b: 2}

  set filter(value: any) {
    this._filter = value;
    this.filterChanges.next(this._filter);
  }

  get filter() {
    return this.filterChanges.asObservable();
  }

Subscribe on:

this.filter.subscribe(model => {
   console.log(model);
});

Changes I make from form:

[(ngModel)]="_filter.teacherId"