9

I'm fairly new to Angular, I work mainly with VueJS. I want to know how I can detect when a variable gets updated. I'm updating my variable through a DataService. I read about ngOnChanges() but I saw that this only works for inputs.

This is pretty much my code:

import { DataService } from "../../service/my.service";

export class MainPage {
  myVar: String = ""; // this is the variable I want to listen when a change is made

   constructor (
    private data: DataService
   ) {}

   ngOnInit() {
    this.data.changeVar.subscribe(message => this.myVar = message);
  }
}

my.service

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class DataService {

  private messageSource = new BehaviorSubject<string>("");
  changeVar = this.messageSource.asObservable();

  constructor() { }

  changeMyVar (message: string) {
    this.messageSource.next(message)
  }

}

This is where I'm updating the variable.

import { DataService } from "../../service/my.service";

export class AnotherPage {
  anotherVar: String = '';

  constructor(
    private data: DataService
  ) { }

  ngOnInit() {
    this.data.changeVar.subscribe(message => this.anotherVar = message)
  }

  myFunction () {
    this.data.changeMyVar("Hello"); // update variable to "Hello"
  }
}

Any help is greatly appreciated! :)

sirzento
  • 537
  • 1
  • 5
  • 23
Brian Moreno
  • 977
  • 4
  • 11
  • 39
  • 1
    Since you assign a new value to `myVar` in the Observable callback, you know when it changes. What other information do you want to get? – ConnorsFan Apr 22 '18 at 03:35
  • I want to run a function in MainPage when the value of the variable has been changed. – Brian Moreno Apr 22 '18 at 12:57

2 Answers2

13

If myVar is changed only in the Observable callback defined in ngOnInit, you can call a method after changing the variable:

export class MainPage {

    myVar: string = "";

    ngOnInit() {
        this.data.changeVar.subscribe(message => {
            if (message !== this.myVar) {
                this.myVar = message;
                this.doSomething();
            }
        });
    }

    private doSomething() {
        ...
    }
}

On the other hand, if myVar can be changed in other places (since it is public), you can access it in a getter/setter, and call the method in the setter:

export class MainPage {

    private _myVar: string = "";

    get myVar(): string {
        return this._myVar;
    }
    set myVar(value: string) {
        if (value !== this._myVar) {
            this._myVar = value;
            this.doSomething();
        }
    }

    ngOnInit() {
        this.data.changeVar.subscribe(message => this.myVar = message);
    }

    private doSomething() {
        ...
    }
}
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
  • Thank you, your example helped a lot! :) – Brian Moreno Apr 22 '18 at 15:20
  • Im trying to use your solution, but I get "Parameter 'message' implicitly has an 'any' type". When I do message: any still does not work. Any suggestion? Thanks! – Danielle Mar 07 '23 at 15:27
  • I think that something like `subscribe((message: string) => ...` would eliminate the error message (assuming that `message` is a string). – ConnorsFan Mar 07 '23 at 18:03
0

You can try use this: https://angular.io/api/core/OnChanges

and get the object changes with: changes.objectName

more details on the angular.io page.

  • 3
    1. Its recommended adding solution as part of your answer along with any external link. 2. Please refer to this for better presenting your question/answer: https://meta.stackoverflow.com/a/251362/3519504 – Sandeep Kumar May 11 '20 at 03:00