简体   繁体   English

Angular - 两路绑定

[英]Angular - Two Way Binding

I am a beginner at Angular and I covering two way binding but for some reason I do not understand what I am doing wrong with the below any input would be appreciated.我是 Angular 的初学者,我介绍了两种方式绑定,但由于某种原因,我不明白我在下面做错了什么,任何输入都将不胜感激。

I am simply trying to understand the concept so the below code is rather simple.我只是想理解这个概念,所以下面的代码相当简单。 Per my understanding据我了解

  1. Adding the two way binding [()] to <app-child-comp> I pass the parent field "name" from the parent component to the parent view and using property binding it provides an initialization value ( default value ) to the child component that receives the value in the @Input field.将双向绑定 [()] 添加到<app-child-comp>我将父字段“名称”从父组件传递到父视图,并使用属性绑定为子组件提供初始化值(默认值)接收@Input 字段中的值。
  2. Once the field "@Input childName " has its value using normal interpolation I can use the value how ever I please in the child template.一旦字段“@Input childName”使用正常插值获得其值,我就可以在子模板中使用我喜欢的值。
  3. Now by defining an EventEmitter and then using its.emit method I should be able to pass any changes on the variable back up to the parent component and update the DOM property to reflect the changes.现在通过定义一个 EventEmitter 然后使用 its.emit 方法,我应该能够将变量上的任何更改传递回父组件并更新 DOM 属性以反映更改。

父组件和模板

子组件和模板

程序输出

在此处输入图像描述

Problem:问题:

Now this is my problem the parent --> child direction the bindings are working as intended, the name "Fin" is appearing as I expect in the input of the parent Template and in the interpolation in the child template but when I want to alter the name in the child template and have it bubble back up to the parent property it fails to update although it updates the interpolation in the child template.现在这是我的问题,父->子方向绑定按预期工作,名称“Fin”出现在父模板的输入和子模板的插值中,但当我想改变子模板中的名称并将其冒泡到父属性,尽管它更新了子模板中的插值,但它无法更新。

Ive been trying to figure this out now for a while and everything im researching I feel says im doing it correctly but if you could please explain what im doing wrong I would much appreciate it.我已经尝试解决这个问题一段时间了,我正在研究的所有内容都说我做对了,但如果你能解释一下我做错了什么,我将不胜感激。

Im adding this so that anyone looking in the future can learn from my mistake.我添加了这个,以便未来的任何人都可以从我的错误中吸取教训。

There are two ways to perform event binding given a child component给定子组件,有两种方法可以执行事件绑定

  1. The first way is by explicitly declaring the property and event bindings as follows <app-child-comp [childName]="name" (childNameChange)="name =$event"></app-child-comp>第一种方法是通过如下显式声明属性和事件绑定<app-child-comp [childName]="name" (childNameChange)="name =$event"></app-child-comp>

  2. The second way is to use the "Banana Box" Method where the child tag transforms into <app-child-comp [(childName)]="name"></app-child-comp>第二种方法是使用“香蕉盒”方法,其中子标签转换为<app-child-comp [(childName)]="name"></app-child-comp>

I was trying to use the second method and something that wasn't immediately clear is that there is a naming convention when it comes to the field names in the child component that needs to be followed in order for the "Banana Method" to work我试图使用第二种方法,但不清楚的是,当涉及到子组件中的字段名称时,需要遵循一个命名约定才能使“香蕉方法”起作用

Rule: If your @Input field is named "x" then your @Output EventEmitter needs to be named "xChange" the "Change" is required as the second part of the name.规则:如果您的@Input 字段被命名为“x”,那么您的@Output EventEmitter 需要被命名为“xChange”,“Change”是名称的第二部分。

Syntax: inputName + Change语法:输入名称 + 更改

So in order to resolve my problem I needed to change the naming convention from因此,为了解决我的问题,我需要更改命名约定

@Input() childName:string;
@Output() changedName = new EventEmitter<string>();

to

@Input() childName:string;
@Output() childNameChange = new EventEmitter<string>();

You have to add the output in your root component:您必须在根组件中添加 output :

<app-child-app [(childName)]="name" (changedName)="name = $event"></app-child-app>

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

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