简体   繁体   English

Angular - 深嵌套 object 模型已链接

[英]Angular - Deep nested object models are linked

I've a Angular application which displays company & contact person information on a text box as below Company Email address:我有一个 Angular 应用程序,它在文本框中显示公司和联系人信息,如下公司 Email 地址:

   <label> Company Email address</label>
   <input type="text" class="form-control" [(ngModel)]="companyInfo.contactInfo.email" value="{{ companyInfo?.contactInfo?.email }}">

&&& Contact Person Email address: &&& 联系人Email地址:

  <label>Contact Email address</label>
  <input type="email" class="form-control" [(ngModel)]="companyInfo.contactPerson.contactInfo.email" value="{{ companyInfo?.contactPerson?.contactInfo?.email }}">

since this info is obtained from a nested object, when the email address of the company is typed in, it reflects in the contact's email address as well.由于此信息是从嵌套的 object 中获得的,因此当输入公司的 email 地址时,它也会反映在联系人的 email 地址中。 Anything I'm overlooking as component assignment works fine but when input is typed in, it gets mirrored?我忽略的任何组件分配都可以正常工作,但是当输入输入时,它会被镜像?

Hi i got your question it is due to two way binding ngModel is 2-way data binding but it seems the values seems different in this case please check proper names or if possible write your problem in stackblitz .嗨,我收到了您的问题,这是由于两种方式绑定ngModel是 2 方式数据绑定,但在这种情况下,值似乎不同,请检查正确名称,或者如果可能的话,在stackblitz中写下您的问题。

Or there is another way for it, assign 2 variables in.ts class and reflect the names like this.或者还有另一种方法,分配 2 个变量 in.ts class 并反映这样的名称。

   .ts class

    companyEmail: any;
    contactEmail: any;
    this.companyEmail = companyInfo.contactInfo.email;
    this.contactEmail = companyInfo.contactPerson.contactInfo.email;

    .html
<-- For Comapny Email -->
   <input type="text" class="form-control" [(ngModel)]="companyEmail" value="{{ companyEmail }}">

<-- For Contact Email -->

<input type="email" class="form-control" [(ngModel)]="contactEmail" value="{{ contactEmail }}">

I am not sure about your object structure, However I have designed an object based on my understanding from your question and tried the below example我不确定您的 object 结构,但是根据我对您的问题的理解,我设计了一个 object 并尝试了以下示例

Stackblitz 堆栈闪电战

But, I didn't see that the second value is getting changed, when i am modifying the first value in UI.但是,当我在 UI 中修改第一个值时,我没有看到第二个值发生了变化。 Its behavior looks good only (You can check the console to verify - i am printing both the values on change call)它的行为看起来不错(您可以检查控制台以验证 - 我正在打印更改调用中的两个值)

Found the issue to be cause of reference to same object while resetting the fields to create a new company form //while resetting发现问题是在重置字段以创建新公司表单时引用相同的 object 的原因 //重置时

newCompany: Company = new Company(companyName, this.contactInfo,new ContactPerson ( lastName, firstName,this.contactInfo)

replaced with替换为

newCompany: Company = new Company(companyName, ._cloneDeep(this.contactInfo),new ContactPerson ( lastName, firstName,._cloneDeep(this.contactInfo))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM