in parent html:
<app-search-form
[query]="queryString"
(query-change)="onSearchFormChange($event)"
>
Search for posts:
</app-search-form>
in child (a generic search-form) ts:
// coming in via the [query] attribute
@Input('query') defaultQuery = '';
// fired up to parent as (query-change) event
@Output('query-change') queryEmitter = new EventEmitter<string>();
// internals
query: FormControl;
// listen for changes and react to them
ngOnChanges (changes) {
if (changes.defaultQuery) {
this.query.setValue(this.defaultQuery);
}
}
❗ One can use getters and setters to emulate computed observable behaviour (eg. modify internal variable B when input A changes), but performance-wise it's NOT recommended.
💣 Circumventing an I/O interface:
<app-timer #timer></app-timer>
) from the parent template is possible.@ViewChild
.This technique may be useful during component testing (creating a temporary wrapper component that can manipulate the subject's internals).
Some of the popular approaches: