Created
August 7, 2022 18:47
-
-
Save Burgov/90af0d4d8fbfa8ff1dc1fd344ac15b73 to your computer and use it in GitHub Desktop.
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
@Component({ | |
selector: 'parent-component', | |
template: '<child-component></child-component>' | |
}) | |
export class ParentComponent implements AfterViewInit { | |
@ViewChild(ChildComponent) child!: ChildComponent; | |
protected myData = 'my data'; | |
handleEvent(counter: number) { | |
alert(counter); | |
} | |
ngAfterViewInit() { | |
this.child.setData(this.myData); | |
this.child.obs$.subscribe(counter => alert(counter)); | |
} | |
} | |
@Component({ | |
selector: 'child-component', | |
template: '<div (click)="emitCounter()">{{ myData }}</div>' | |
}) | |
export class ChildComponent { | |
data: string; | |
private sub = new Subject<number>(); | |
obs$ = sub.asObservable(); | |
private counter = 0; | |
setData(data: string) { | |
this.data = data; | |
} | |
emitCounter() { | |
this.sub.next(++counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment