I'm relatively new to Angular and I'm looking to achieve the above with reactive forms. I think I'm getting confused between component design and reactive form patterns.
Questions
The first time we load the parent component, with url params, it will pass the value to the child component and the search text box will have the correct value. If I click back on the browser and the parent updates it's searchTerm, which is passed to the child component again, it will never update the search text box as child.ngOnInit() will not execute again.
Is this even a valid design having the formGroup declared in the child component? Most examples I've seen have a formGroup declared in the parent and then include a child component which then has 1 or many formControls....but doesn't that mean that now any other component that wants to include my child component must now declare a formGroup, the child component would seem less independent.
Parent View
<searchBox (search)="onSearchTextEntered($event)" [searchText]="this.searchText"></searchBox>
Parent Component
ngOnInit() {
/*
1) check url params, execute search, set searchText value that is passed to childComponent
so it can populate searchBox text
2) Subscribe to NavigateEnd event of router to catch back/fwd browser click, execute search, set searchText value that is passed to childComponent
so it can populate searchBox text
*/
}
Child View
<input type="text" [formControl]="searchTerm">
Child Component
@Input() searchText: string;
@Output() search: EventEmitter<string> = new EventEmitter();
searchSection = new FormGroup({
searchTerm: new FormControl()
ngOnInit() {
this.searchSection.get('searchTerm').setValue(this.searchText);
}
...