【发布时间】:2026-02-04 04:35:01
【问题描述】:
给定以下路径:
const appRoutes: Routes = [
{ path: '', component: ListComponent },
{ path: 'items/:id', component: ItemOutletComponent,
children:[
{ path: '', component: Section1Component },
{ path: 'page2', component: Section2Component, },
]
}];
如何确保每次在 Section1Component 和 Section2Component 上调用一个名为 applyChanges() 的自定义方法:
- 用户离开页面
-
:idurl 参数变化 - 作为附加问题,当用户关闭浏览器时也是如此?
我目前在ItemOutletComponent加载资源模型,我在每个section组件上创建FormGroup:
ngOnInit() {
this.formGroup = new FormGroup({
section1: new FormGroup({
prop1: new FormControl(),
prop2: new FormControl()
});
this.formGroup.pathValue(this.model);
}
applyChanges() {
Object.assign(this.model, this.formGroup.value);
}
我只需要确保在适当的时间将 formGroup 更改应用回模型。页面上没有保存按钮。
【问题讨论】:
标签: angular typescript angular-routing angular-reactive-forms angular-forms