Last active
March 1, 2020 15:55
-
-
Save munkacsitomi/152a37e4e3f2a70cb2f53767ca45e67d to your computer and use it in GitHub Desktop.
Angular RxJS BehaviorSubject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
@Component({ | |
selector: 'app-root', | |
template: ` | |
<app-parent></app-parent> | |
<app-unrelated></app-unrelated> | |
`, | |
styles: [] | |
}) | |
export class AppComponent {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BrowserModule } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { FormsModule } from '@angular/forms'; | |
import { AppComponent } from './app.component'; | |
import { ParentComponent } from './components/parent/parent.component'; | |
import { SiblingComponent } from './components/sibling/sibling.component'; | |
import { UnrelatedComponent } from './components/unrelated/unrelated.component'; | |
import { SharedService } from './services/shared.service'; | |
@NgModule({ | |
declarations: [ | |
AppComponent, | |
ParentComponent, | |
SiblingComponent, | |
UnrelatedComponent, | |
], | |
imports: [ | |
BrowserModule, | |
FormsModule | |
], | |
providers: [SharedService], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { SharedService } from '../../services/shared.service'; | |
@Component({ | |
selector: 'app-parent', | |
template: ` | |
<h1>Parent Component</h1> | |
<p>{{ message }}</p> | |
<app-sibling></app-sibling> | |
`, | |
styles: [] | |
}) | |
export class ParentComponent implements OnInit { | |
message: string; | |
constructor(private sharedService: SharedService) {} | |
ngOnInit() { | |
this.sharedService.currentMessage$.subscribe(message => (this.message = message)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { SharedService } from '../../services/shared.service'; | |
@Component({ | |
selector: 'app-sibling', | |
template: ` | |
<h1>Sibling Component</h1> | |
<p>{{ message }}</p> | |
<input type="text" (keyup)="onTextChange($event.target.value)"> | |
`, | |
styles: [] | |
}) | |
export class SiblingComponent implements OnInit { | |
message: string; | |
constructor(private sharedService: SharedService) {} | |
ngOnInit() { | |
this.sharedService.currentMessage$.subscribe(message => (this.message = message)); | |
} | |
onTextChange(newMessage) { | |
this.sharedService.changeMessage(newMessage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment