微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在保持 FormGroup 内容活跃的同时重新加载页面

如何解决如何在保持 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?