简体   繁体   English

Angular 2教程 - 对绑定的误解

[英]Angular 2 tutorial - Misunderstanding with bindings

The question is at the end. 问题是最后的问题。

I'm learning Angular (angular 2). 我正在学习Angular(角度2)。 I know already Angular 1.5. 我已经知道Angular 1.5了。

I'm doing the official tutorial of angular and I don't understand one point at the chapter 3.Master/Detail. 我正在做角度官方教程,我不明白第3章中的一点。主人/细节。

Here is the live example 这是现场的例子

CODE1 CODE1

<li *ngFor="let hero of heroes"
    (click)="onSelect(hero)">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
</li>

When we click on the hero, we lunch the function onSelect(hero) 当我们点击英雄时,我们在onSelect(英雄)上午餐

onSelect(hero: Hero): void {
    this.selectedHero = hero;
}

Then, the following code appears. 然后,出现以下代码。

CODE2 CODE2

<div *ngIf="selectedHero">
    <h2>{{selectedHero.name}} details!</h2>
    <div><label>id: </label>{{selectedHero.id}}</div>
    <div>
        <label>name: </label>
        <input [(ngModel)]="selectedHero.name" placeholder="name"/>
    </div>
</div>

My question: When I change the value of the input with the [(ngModel)] , the text of the CODE1 change. 我的问题:当我用[(ngModel)]更改输入的值时,CODE1的文本会发生变化。 Why? 为什么? Because I have tried with a similar code with angular 1.5 and the text of CODE1 doesn't update. 因为我尝试使用角度为1.5的类似代码,并且CODE1的文本不会更新。

I want to change with the input, the text of {{selectedHero.name}} and not {{hero.name}} 我想更改输入,{{selectedHero.name}}的文本,而不是{{hero.name}}

  • SelectedHero is equal to hero but not the opposite isn't, is it? SelectedHero等于英雄而不是相反不是,是吗?
  • How to give selectedHero the value of hero with onSelect() and not to affect hero when i update selectedHero with the input (but change of course {{selectedHero.name}} )? 如何使用onSelect()赋予selectedHero 英雄的价值,而不是在我用输入更新selectedHero时影响英雄 (但更改当然{{selectedHero.name}} )?

EDIT: Because of the Misunderstanding of my question, I try to reformulate: 编辑:由于对我的问题的误解,我尝试重新制定:

When I change the value in the input [(ngModel)]="selectedHero.name", I want to change the value of {{selectedHero.name}} but not to change the value of {{hero.name}}. 当我更改输入[(ngModel)] =“selectedHero.name”中的值时,我想更改{{selectedHero.name}}的值,但不要更改{{hero.name}}的值。 How can I succeed that? 我怎么能成功呢? Thank you. 谢谢。

EDIT 2 QT Ray gives the Answer in comments. EDIT 2 QT Ray在评论中给出答案。

We have to copy hero to succeed. 我们必须复制英雄才能成功。

We need to replace 我们需要更换

this.selectedHero = hero;

by 通过

this.selectedHero = Object.assign({}, hero);

or with deep copy (with lodash) 或者使用深层复制(使用lodash)

this.selectedHero = _.cloneDeep(hero)

SelectedHero is equal to hero but not the opposite isn't, is it? SelectedHero等于英雄而不是相反不是,是吗?

In that situation, yes. 在那种情况下,是的。 SelectedHero and hero are the same. SelectedHero和英雄是一样的。 SelectedHero doesn't exist without selecting a hero. 如果没有选择英雄,SelectedHero就不存在。

When your code is loaded, selectedHero is probably something like: 加载代码后,selectedHero可能类似于:

public selectedHero: IHero

And in your constructor, you may have 在你的构造函数中,你可能有

this.selectedHero = null

selectedHero is not defined until you click a hero and then they become the same thing. 在您单击一个英雄然后它们变成同一个东西之前,不会定义selectedHero。

You could select another hero and then that Hero becomes the selectedHero. 您可以选择另一个英雄,然后Hero成为selectedHero。

How to give selectedHero the value of hero with onSelect() and not to affect hero when i update selectedHero with the input (but change of course {{selectedHero.name}})? 如何使用onSelect()赋予selectedHero英雄的价值,而不是在我用输入更新selectedHero时影响英雄(但更改当然{{selectedHero.name}})?

You would need to get rid of the () around the ngModel. 您需要摆脱ngModel周围的()。 So it should say [ngModel]. 所以应该说[ngModel]。 The () binds the ngModel directive to the event. ()将ngModel指令绑定到事件。 The [] lets the element know that it's an attribute. []让元素知道它是一个属性。 The click is fine, you will still pass that information to the element. 单击很好,您仍然会将该信息传递给元素。

Excerpt from here: https://angular.io/docs/ts/latest/api/forms/index/NgModel-directive.html 摘自此处: https//angular.io/docs/ts/latest/api/forms/index/NgModel-directive.html

If you have a one-way binding to ngModel with [] syntax, changing the value of the domain model in the component class will set the value in the view. 如果使用[]语法对ngModel进行单向绑定,则更改组件类中域模型的值将在视图中设置值。 If you have a two-way binding with [()] syntax (also known as 'banana-box syntax'), the value in the UI will always be synced back to the domain model in your class as well.` 如果您使用[()]语法(也称为“banana-box syntax”)进行双向绑定,则UI中的值也将始终同步回您班级中的域模型。

You are using two way binding which is why the hero you selected in CODE1 changes. 您正在使用双向绑定,这就是您在CODE1中选择的英雄发生变化的原因。

I hope this helps. 我希望这有帮助。

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

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