如何解决如何在保持 FormGroup 内容活跃的同时重新加载页面
我的应用程序的 sidenav 中有一个过滤器功能,用于过滤配置文件列表。然而,当我在 HTML 中提交过滤器函数时,我调用这个 onFilterButtonSubmit 函数(见下文),它应该为我的 profile-list.component.ts 提供参数,同时重新加载页面。
onFilterButtonSubmit(minA,maxA,gen): void {
this.profileService.minAge = minA;
this.profileService.maxAge = maxA;
this.profileService.gender = gen;
this.router.navigate(['/home']);
}
该站点并未重新加载。只有当我在另一个页面(例如个人资料页面)解析过滤条件并单击过滤器时,它才会正确地将我重新路由到 /home 并显示我过滤的结果。我还尝试使用 window.location.reload();
成功重新加载页面但不保存 FormField 内容。我有点迷失在这里,任何帮助表示赞赏。不确定哪些其他代码部分是相关的,但请告诉我,我会将其添加到问题中。
我使用 Angular 和 Django 作为后端。
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,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']);
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。