我有一个拥有一些属性的用户的数组。当用户更改路由时,它应该只显示那些具有某些特定属性的用户,这些属性放在url中。目前,我只能在任何地方显示所有用户,而且当id被更改时,组件似乎不会改变。
我试过了
ngOnInit(): void {
const id = +this.route.snapshot.paramMap.get('departmentId');
this.employeeService.getEmployees()
.subscribe(emps => this.employees = emps);
this.employees = this.employees.map(emp => {
if (emp.departmentId === id) { return emp; }
});
}发布于 2018-01-20 10:24:02
订阅route.params以处理更改。然后注意,getEmployees()是异步的。因此,当您尝试筛选时,变量this.employees可能还没有初始化。你可以在订阅里面过滤。
做这样的事:
ngOnInit(): void {
this.route.params.subscribe(params => {
const id = +params['departmentId'];
this.employeeService.getEmployees().subscribe(emps =>
this.employees = emps.filter(emp => emp.departmentId === id)
);
}
}https://stackoverflow.com/questions/48354845
复制相似问题