-1

I want to update a variable each time after it changes in angular view.

How can it be achieved in angular without using @input decorator?

Leon Bambrick
  • 26,009
  • 9
  • 51
  • 75

1 Answers1

0

If your variable is called title in your component then you can do this:

  this.title.valueChanges
      .debounceTime(500)
      .distinctUntilChanged()
      .subscribe (newValue => {
        // do something based on the newvalue 
    });

However, you now have a subscriber that will fire every time the title changes. So you should really destroy it at some point, or you will get memory leak/stack overflow eg:

this.title.valueChanges
      .debounceTime(500)
      .distinctUntilChanged()
      .takeUntil(this.destroyValueChanges$)
      .subscribe (newValue => {
        // do something based on the newvalue 
    });

Delcare destroyValueChanges$ as a Subject property of your component and in ngOnDestroy call destroyValueChanges$.next();

NOTE

Since you did not post any code, I have to guess what you are asking. I am assuming that title is an <input> on your template, in which the user is typing and hence that is how the value is changing. If the variable you are talking about is being changed 'outside' your component, then it needs to be have an @Input decorator and you need to use the ngOnChanges life-cycle hook.

rmcsharry
  • 5,363
  • 6
  • 65
  • 108