Skip to content

Instantly share code, notes, and snippets.

@Burgov
Created August 7, 2022 18:47
Show Gist options
  • Save Burgov/90af0d4d8fbfa8ff1dc1fd344ac15b73 to your computer and use it in GitHub Desktop.
Save Burgov/90af0d4d8fbfa8ff1dc1fd344ac15b73 to your computer and use it in GitHub Desktop.
@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