[英]Angular2: When doing two-way binding, do I need to explicitly use an EventEmitter?
Suppose that I have a Kitchen class that looks like this: 假设我有一个如下所示的Kitchen类:
@Component({
template: `
<kitchen [(kitchenLunch)]=lunch></kitchen>
`
})
export class House {
private lunch: Lunch;
}
The House component: 房屋部分:
Kitchen
包含子组件Kitchen
My question is on the syntax of the @Output
in the Kitchen. 我的问题是厨房中@Output
的语法。 This is currently what I am using: 这是我目前正在使用的:
@Component({
selector: 'kitchen',
template: '...'
})
export class Kitchen {
@Input
private kitchenLunch: Lunch;
@Output
private kitchenLunchChange: EventEmitter<any> = new EventEmitter();
}
This currently works as I expect. 目前,这符合我的预期。 Whenever I update the kitchenLunch in the Kitchen
component, I do the following: 每当我更新Kitchen
组件中的kitchenLunch时,我都会执行以下操作:
this.kitchenLunch = **Something**
this.kitchenDataModelChange.next(this.kitchenDataModel);
However, this seems slightly redundant. 但是,这似乎有点多余。 What I would really like to do is shorten the Kitchen class to do something like this: 我真正想做的是缩短Kitchen班,做这样的事情:
@Component({
selector: 'kitchen',
template: '...'
})
export class Kitchen {
@Input
@Output
private kitchenLunch: Lunch;
}
Then when I update kitchenLunch, I would like to just do this: 然后,当我更新kitchenLunch时,我只想这样做:
this.kitchenLunch = **Something**
Questions: 问题:
@Output
must be named the same as the input plus Change
. 看起来@Output
必须与输入加上Change
命名相同。 In this case kitchenLunch + Change
. 在这种情况下, kitchenLunch + Change
。 I figured this out by trial and error. 我通过反复试验弄清楚了这一点。 Where is this documented? 这在哪里记录? 1) don't know but I guess it works. 1)不知道,但我猜它可行。 Should be easy to find out. 应该很容易发现。
2) no 2)没有
3) no 3)没有
4) https://angular.io/docs/ts/latest/guide/template-syntax.html 4) https://angular.io/docs/ts/latest/guide/template-syntax.html
[(ngModel)] is a specific example of a more general pattern in which Angular "de-sugars" the [(x)] syntax into an x input property for property binding and an xChange output property for event binding. [(ngModel)]是更通用模式的特定示例,其中Angular将[[x]]语法“解糖”为用于属性绑定的x输入属性和用于事件绑定的xChange输出属性。 Angular constructs the event property binding's template statement by appending =$event to the literal string of the template expression. Angular通过将= $ event附加到模板表达式的文字字符串来构造事件属性绑定的模板语句。
[(x)]="e" <==> [x]="e" (xChange)="e=$event" [(x)] =“ e” <==> [x] =“ e”(xChange)=“ e = $ event”
We can write a two-way binding directive of our own to exploit this behavior. 我们可以编写自己的双向绑定指令来利用此行为。
You don't have to use Ouput
EventEmitter
with two-way binding. 您不必使用Ouput
EventEmitter
具有双向约束力。
However, it's recommended to use one-way binding together with EventEmitter
because it may give you better performance. 但是,建议将单向绑定与EventEmitter
一起使用,因为它可以为您提供更好的性能。 Briefly, it reduces amount of change detection Angular 2 should perform. 简而言之,它减少了Angular 2应该执行的变更检测量。
More details here http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/ 更多详细信息,请参见http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.