[英]In Angular, can I bind the emited output of a component directly to a property?
My main application component interacts with sub components through @Output
decorated properties on the subcomponent.我的主要应用程序组件通过子组件上的
@Output
装饰属性与子组件交互。 The output properties use and EventEmitter<>()
. output 属性使用
EventEmitter<>()
。 Often the property emits a simple boolean or number.该属性通常会发出一个简单的 boolean 或数字。 I would like to bind this output directly to properties in the main application.
我想将此 output 直接绑定到主应用程序中的属性。 But am failing to do so.
但是我没有这样做。
What I am doing at the moment is:我现在正在做的是:
//In my sub component:
@Output() subProperty = new EventEmitter<boolean>();
//In my main template:
<sub-component (subProperty)="setPropertyValue($event)"></subcomponent>
//In my main component (this I would like to avoid):
setPropertyValue(event) {
this.mainProperty = event;
}
What I wanted to do was avoid the function in my main component and bind directly to my property, but the below code doesn't work:我想做的是避免在我的主要组件中使用 function 并直接绑定到我的属性,但是下面的代码不起作用:
//In my sub component:
@Output() subProperty = new EventEmitter<boolean>();
//In my main template:
<sub-component (subProperty)="mainProperty"></subcomponent>
Is there anyway I can avoid the additional function in my main component?无论如何我可以避免在我的主要组件中添加额外的 function 吗?
我相信您能做的最好的是:
(subProperty)="mainProperty = $event"
Add the suffix Change
to the end of your property so you can use the banana/football in the box syntax.将后缀
Change
添加到您的属性的末尾,以便您可以在框语法中使用 banana/football。 A matching input is not required and will be ignored.匹配的输入不是必需的,将被忽略。
//In your sub component:
@Output() subPropertyChange = new EventEmitter<boolean>();
//In your main template:
<sub-component [(subProperty)]="mainProperty"></subcomponent>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.