问题描述
我的应用程序的 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 (将#修改为@)