简体   繁体   English

为什么 angular2 组件之间的单向绑定对对象不起作用?

[英]Why angular2 one-way binding between components works wrong with objects?

I have parent and child components.我有父组件和子组件。
I want to send form's values from parent to child component.我想将表单的值从父组件发送到子组件。
Child component can do everything with this data, but all changes are local and should not be returned back to parent component.子组件可以使用这些数据做任何事情,但所有更改都是本地的,不应返回给父组件。

When i send a simple variable to child comp – everything works fine and changes doesn't returned back to parent.当我向子组件发送一个简单的变量时- 一切正常,更改不会返回给父组件。
But when i send form's values – all changes returns back to parent component...但是当我发送表单的值时- 所有更改都返回到父组件......

Live居住

https://stackblitz.com/edit/angular-dvqyjr https://stackblitz.com/edit/angular-dvqyjr

Parent Component JS父组件 JS

export class AppComponent  {
constructor (private formBuilder: FormBuilder){};

simpleVariable = 'Value';
form: FormGroup = this.formBuilder.group({
    id: 1,
    name: 'Name'
});

Parent Component HTML父组件 HTML

<hello [simpleVariable]="simpleVariable" [form]="form.value"></hello>

Child Component JS子组件 JS

export class HelloComponent  {
  @Input() simpleVariable;
  @Input() form;
}

Child Component HTML子组件 HTML

<input [(ngModel)]="simpleVariable">
<input [(ngModel)]="form.name">

Question

So how can i send an object to child component and modify it without returning data to parent?那么如何将对象发送到子组件并修改它而不将数据返回给父组件?

This is quite simple.这很简单。 The reason for this behavior is that the form.value is an object.这种行为的原因是form.value是一个对象。 Meaning that you're sharing a reference object with parent and child.这意味着您正在与父母和孩子共享一个参考对象。 Meaning that any change to this object will result in a change in both parent and child.这意味着对此对象的任何更改都将导致父项和子项的更改。

In order to make changes in child that won't affect your parent, create a deep copy of your object using Object.assign function and use it in your child component.为了对子组件进行不影响父组件的更改,请使用Object.assign函数创建对象的深层副本,并在子组件中使用它。

export class HelloComponent implements OnInit {
  _from: {};
  @Input() simpleVariable;
  @Input() form;

  ngOnInit() {
    this._form = {};
    Object.assign(this._from, this.form);
  }
}

Forked and edited example分叉和编辑的示例

From the Angular documentation at Reactive Forms - Step 2: Associating the FormGroup model and view :来自Reactive Forms的 Angular 文档- 第 2 步:关联 FormGroup 模型和视图

A form group tracks the status and changes for each of its controls, so if one of the controls changes, the parent control also emits a new status or value change.表单组跟踪其每个控件的状态和更改,因此如果其中一个控件发生更改,父控件也会发出新的状态或值更改。

You can try the following one with the form:您可以尝试使用以下表单:

<form [formGroup]="form">
  <input formControlName="name">
</form>

It all comes to Explaining Value vs. Reference in Javascript as well as Angular's Change Detection这一切都涉及在 Javascript 中解释值与引用以及Angular 的更改检测

Overall, @benshabatnoam already answered you'r question - in order to execute change detection - you need to change object reference, either by old fashioned way:总的来说,@benshabatnoam 已经回答了你的问题——为了执行更改检测——你需要通过老式的方式更改对象引用:

Object.assign(this._from, this.form);

Or ES6 way:或者 ES6 方式:

this._from = {...this.form}

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

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