【发布时间】:2019-06-12 22:56:24
【问题描述】:
我有一个角度反应形式的电子邮件字段,如果表单处于编辑模式(与添加模式相反),我希望禁用该字段。我正在使用
disabled: typeof user.user_id === 'string'
如果我在下面的modelToForm 方法的开头设置一个断点并将typeof user.user_id === 'string' 粘贴到控制台中,它会返回true,所以它看起来应该可以工作,但表单中的字段没有被禁用。如果我将其更改为“禁用:true”,则该字段被禁用。有人遇到过这个吗?
public modelToForm(user: UserModel): FormGroup {
return this.fb.group({
firstName: [user.first_name, [
Validators.required,
Validators.maxLength(UserFormHelper.maxLengthName),
UserFormHelper.validateWhitespace
]],
email: [{value: user.email, disabled: typeof user.user_id === 'string'}, [
Validators.required,
Validators.maxLength(UserFormHelper.maxLengthEmail),
Validators.pattern(UserFormHelper.emailPattern)
]],
});
}
【问题讨论】:
-
你确定
typeof user.user_id === 'string'是你当时想要的条件吗? -
它应该只适用于 - 'disabled: user.user_id',但使用 typeof user.user_id === 'string' 也应该有效。每当我在方法开始处设置断点并点击编辑时,条件似乎为真(只要我点击添加,它就为假)
标签: angular typescript angular-reactive-forms