NGXS Forms - 如何处理嵌套表单组件
我有一个包含多个步骤的用户配置文件表单,即一个向导。提交/保存时,必须将整个用户个人资料表单提交到 API 我正在使用 NGXS 和 NGXS 表格。
目前,我已将 UI 实现为单个 Angular Reactive Forms 和单个状态属性 form
,该模型是 API 期望的类型。每个步骤都有一个组件,但它们都使用相同的表单
。 这种方法的一个大问题是我无法检测步骤上的字段是否有效,我只能检测整个表单是否有效。
为了解决这个问题,我想我必须创建多个 Angular 表单。但是,我无法将它们全部绑定到同一个状态属性form
,因为在每个步骤中我只有属性的子集。
因此,我现在的想法是在我的状态中为每个步骤创建一个表单属性,每个步骤都有自己的用于存储属性的接口。然后,我可以创建一个选择器,它将返回我可以提交给 API 的整个对象。
目前在代码中:
form: {
model: UserProfileDto,
....
}
提案:
personalDetailsForm: {
model: PersonalDetailsModel,
},
workDetailsForm: {
model: WorkDetailsModel
}
@Selector(ProfileDetails)
profileDetails(state: ProfileState) {
return {...state.workDetailsForm.model, ...state.personalDetailsForm.model}
}
这是可行的方法还是有更简单的方法?
I've got a user profile form with multiple steps, i.e. a wizard. On Submit/Save, the entire user profile form has to be submitted to an API
I'm using NGXS and NGXS Forms.
At the moment I have implemented the UI as a single Angular Reactive Forms and a single state property form
, the model is of the type the API expects. There is a component for each step, but they all use the same form
.
The big problem with this approach is that I can't detect whether the fields on a step are valid or not, I can only detect if the entire form is valid.
To solve this, I think I have to create multiple Angular forms. However, I can't bind them all to the same state property form
, as on each step I only have a subset of properties.
So, my thinking now is to create a form property in my state for each step, each with its own interface for the properties it stores. Then, I can create a selector that will return the entire object I can submit to the API.
In code, currently:
form: {
model: UserProfileDto,
....
}
Proposal:
personalDetailsForm: {
model: PersonalDetailsModel,
},
workDetailsForm: {
model: WorkDetailsModel
}
@Selector(ProfileDetails)
profileDetails(state: ProfileState) {
return {...state.workDetailsForm.model, ...state.personalDetailsForm.model}
}
Is this the way to go or is there an easier approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个非常意见问题,但我可以分享我的想法。
在父组件中,我将为每个步骤创建单独的表单:
It's very opinion question, but I can share my thoughts.
In parent component I will create separate form for every step: