I have got a filter function in a sidenav in my application which filters the list of profiles. However when I submit the filter function in the HTML I call this onFilterButtonSubmit function (see below) which should give my profile-list.component.ts the parameters while also reloading the page.
onFilterButtonSubmit(minA, maxA, gen): void {
this.profileService.minAge = minA;
this.profileService.maxAge = maxA;
this.profileService.gender = gen;
this.router.navigate(['/home']);
}
The site is not being reloaded though. Only when I am on another page (f.e. profile-page), parsing the filter criteria and clicking on Filter, it correctly reroutes me to /home and displays the results I filtered for. I also tried using window.location.reload();
which reloads the page successfully but does not save the FormField content. I am kinda lost here, any help is appreciated. Not sure which other code parts are relevant but let me know and I will add it to the question.
I am using Angular and Django for the backend.
app.component.ts
export class AppComponent implements OnInit {
title = 'frontend';
isLoggedIn = false;
filteredFormControl: any = new FormControl('');
filteredProfilesFormGroup: FormGroup;
profiles: Profile[];
public isFiltered: boolean;
constructor(public userService: UserService, public profileService: ProfileService, private router:
Router) {
}
ngOnInit(): void {
this.filteredProfilesFormGroup = new FormGroup({
minAge: new FormControl(''),
maxAge: new FormControl(''),
gender: new FormControl('')
});
this.userService.isLoggedIn.subscribe((isLoggedIn) => {
this.isLoggedIn = isLoggedIn;
});
}
onFilterButtonSubmit(minA, maxA, gen): void {
this.profileService.minAge = minA;
this.profileService.maxAge = maxA;
this.profileService.gender = gen;
this.router.navigate(['/home']);
}
profile-list.component.ts
profiles: Profile[];
filteredProfiles: Profile[];
filteredFormControl = this.appComponent.filteredFormControl;
minAge = '';
maxAge = '';
gender = '';
constructor(private profileService: ProfileService,
public userService: UserService,
public dialog: MatDialog,
public appComponent: AppComponent,
private router: Router) {
}
ngOnInit(): void {
this.minAge = this.profileService.minAge;
this.maxAge = this.profileService.maxAge;
this.gender = this.profileService.gender;
this.filterCustom(this.minAge, this.maxAge, this.gender);
this.filteredFormControl.valueChanges
.subscribe((newValue: string) => {
this.filter(newValue);
});
}
filterCustom(gender: string, minAge: string, maxAge: string): void {
this.profileService.testFilter(gender, minAge, maxAge)
.subscribe((profiles: Profile[]) => {
this.profiles = profiles;
this.router.navigate(['/home']);
});
}
question from:
https://stackoverflow.com/questions/66051116/how-to-reload-page-while-keeping-formgroup-content-alive 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…