Hello I am basically new to Angular, Here I'm trying to access a boolean variable from service. A boolean variable with value true, I've declared in service. Based on some logic I'm going to change that service variable to false. That particular variable I'm using in several components. So I'm facing problem like, Initial value which I've assigned true is coming to all components where I've used, but after changing the value that is not reflecting in all components. Service class like this:
@Injectable()
export class MyService {
running: boolean = true;
}
Component 1:
export class FirstComponent {
constructor(private myService: MyService) { }
check(){
this.myservice.running = false;
}
}
Component 2:
export class SecondComponent implements OnChanges {
running;
constructor(private myService: MyService) { }
ngOnChanges(){
this.running = this.myService.running;
console.log('running', this.running);
}
}
This is the how I'm going to change boolean value and accessing from service in different components. I'm not sure whether this is correct approach or not, please correct me.
Subject
for this. Read more here blog.angularindepth.com/… – Amit Chigadani