Skip to content

Instantly share code, notes, and snippets.

@anmolio
Last active May 10, 2020 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anmolio/8078d6e9d029be191935df9d9ffdee40 to your computer and use it in GitHub Desktop.
Save anmolio/8078d6e9d029be191935df9d9ffdee40 to your computer and use it in GitHub Desktop.
ComponentTwo : Unrelated Components subscribing to data using a serivce (BehaviorSubject)
import { Component, OnInit } from '@angular/core';
import { StatusService } from "../status.service";
@Component({
selector: 'app-componenttwo',
template: `
The status id - {{status}}
<button (click)="changeStatus()">Change Message</button>
`,
styleUrls: ['./sibling.component.css']
})
export class CompomemtTwoComponent implements OnInit {
stauts:string;
constructor(private data: SatatusService) { }
ngOnInit() {
this.data.currentStatus.subscribe(status => this.status = status)
}
changeStatus() {
this.data.changeStatus("ON")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment