angular8 - 通过赋值和使用 patchValue() 为 formGroup 对象设置值有什么区别?
问题描述
如果我使用 Reactive Form 的 patchValue() 函数为我的对象赋值,如果我通过赋值运算符为它赋值会有什么不同?
我已经运行了代码并将值设置为“客户”对象,即:两者的工作方式相似。有人可以告诉我是否有任何我遗漏的细微潜在差异,或者是否有任何我没有得到的明显差异?
客户类:customer.ts
export class Customer {
id: number;
name: string;
age: number;
active: boolean;
myproduct: {
p_id : number;
}
}
客户对象:create-customer-component.ts
customer = this.fb.group({
id: null,
name : ['', [Validators.required, Validators.minLength(4)]],
age : ['', [Validators.required, Validators.min(18),Validators.max(100)]],
active: false,
myproduct:this.fb.group(
{ p_id : null
})
})
上述两个选项
this.customer.value.myproduct.p_id = 1;
this.customer.patchValue({p_id : 2}) ;
解决方案
您可以设置值,但它不会显示在输入框中,或者您可以使用 this.customer.controls['p_id '].setValue('Any Id');
推荐阅读
- python - Python折线图
- regex - 遍历文本文件并从文本文件中提取一组 100 个 IP 并输出到单独的文本文件
- php - 此代码中的 setcookie() 函数是否存在问题?
- c# - 反序列化不同类型的 JSON 数组
- javascript - 访问嵌套函数中的全局变量
- powershell - 我的 powershell 配置文件中的功能不显示?
- mysql - 从学生和教师表中选择
- c++ - 制作完成后是否可以将节点添加到图表中?
- python - Python input() 调用阻止其他线程打印到控制台
- c# - 将 C# PageAsyncTask() 转换为 VB.Net 等效的问题