我有一个在角度 4 中声明的反应式
this.userForm = this.fb.group({
"id": [user ? user.id : ""],
"profile": this.fb.group({
"email": [user && user.profile.email ? { value: user.profile.email, disabled: true } : "", Validators.compose([Validators.required, Validators.email])],
}),
"username": [user && user.username ? { value: user.username, disabled: true } : "", Validators.compose([Validators.required, GlobalValidator.usernameFormat])],
})
默认情况下,如果我的表单填写了数据,则表单字段被禁用(电子邮件、用户名)。我面临的问题是,如果我刷新页面,表单字段将显示为已启用。
(已编辑)
ngOnInit() {
this.buildForm()
if (this.activatedRoute.snapshot.params.id) {
this.service.getUser(Number(this.activatedRoute.snapshot.params.id)).subscribe((user) => {
this.buildForm(user)
})
}
}
buildForm(user?:any){
this.userForm = this.fb.group({
"id": [user ? user.id : ""],
"profile": this.fb.group({
"email": [user && user.profile.email ? { value: user.profile.email, disabled: true } : "", Validators.compose([Validators.required, Validators.email])],
}),
"username": [user && user.username ? { value: user.username, disabled: true } : "", Validators.compose([Validators.required, GlobalValidator.usernameFormat])],
})
}