简体   繁体   English

在 Angular 中,我可以将组件发出的 output 直接绑定到属性吗?

[英]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.

相关问题 Angular 组件:我可以使用 Observable 而不是 EventEmitter 作为 @Output() 属性吗? - Angular component: Can I use an Observable instead EventEmitter as @Output() property? 无法绑定,因为它不是角度分量的已知属性 - Can't bind since it isn't a known property of angular component Angular 2 - 如何将SVG属性数据绑定到组件属性? - Angular 2 - how do I data bind an SVG attribute to a component property? 如何在 angular8 路由器事件中将函数与组件绑定? - How can I bind functions with component in angular8 router events? 我可以从事件绑定到自定义组件输出吗 - can i bind from event to custom component output 无法在Angular 4中绑定属性 - Can't Bind Property in Angular 4 Angular 4无法绑定 <property> 因为它不是一个已知的属性 <component> - Angular 4 Can't bind to <property> since it isn't a known property of <component> Angular 7 无法绑定到<property>因为它不是一个已知的属性<component> (来自导入的模块) - Angular 7 Can't bind to <property> since it isn't a known property of <component> (from imported module) Angular2 RC5:无法绑定到“属性 X”,因为它不是“子组件”的已知属性 - Angular2 RC5: Can't bind to 'Property X' since it isn't a known property of 'Child Component' 无法绑定到属性,因为它不是Angular中“ component”的已知属性 - Can't bind to property since it isn't a known property of 'component' in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM