How can a variable always be checked?


#1

Hello i am loolinhg for a solution for that ;

I have integer variable and i always want to check it weather it is zero or not ?
like subscribe it !

BUT how can i do this

Thanks in Advance


#2

Where do you want to do this? If “from a template”, then just let Angular’s change detection deal with it:

<div *ngIf="!foo">foo is zero</div>

#3

what about at ts file ? it s like kind of a state controll but basic just one variable


#4

Check it in your constructor for example, eg, if this.myvalue != “null || undefined” { this.myvalue = something }.


#5

You can check that variable within your ts file.

import {Observable } from 'rxjs/Rx';
@Component({
  ...
})
constructor(){
    Observable.interval(100).subscribe(x => {
        if(variable == 0)
        {
            //do this
        }
        else 
        {   //do that 
        }
    });
}

#6

However this checks only once ! it need to be like state controller .


#7

@techsnake you’re right, then work with @amitk04 answer just above :slight_smile: (an observable or promise)


#8

Please describe what you are actually trying to achieve. Periodically polling like the suggestion given above involving interval is a wasteful use of resources.