I'm trying to subscribe the changes of variable from angular service without to subscribe it in the component. Here is my code which I tested:
Service:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class testService {
detectChanges = new Subject();
myObject : any = {
test:"Test"
};
constructor() {
this.detectChanges.subscribe((data) => {
console.log("changed");
});
}
}
Component:
import { Component } from '@angular/core';
import { testService } from './test-service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
$s;
constructor(private test: testService) {
this.$s = this.test.myObject;
}
}
html:
<input [(ngModel)]="$s.test">
<h1>{{$s.test}}</h1>
Is there any way to do that? like in angularjs $watch
Here is an example https://stackblitz.com/edit/angular-cmyy66?file=src%2Fapp%2Fapp.component.html
In the example myObject will changes using an input
BehaviorSubject(which receives push updates), rather than a planSubject- Z. Bagley