[英]how to input values in Angular component used as attribute
I'm trying to create a component and I want to use it as an attribute on a div to remove the component tag, but this component requires inputs. 我正在尝试创建一个组件,并且想将其用作div上的属性以删除组件标签,但是该组件需要输入。 My question is how to inject those inputs. 我的问题是如何注入这些输入。
Using a component tag: 使用组件标签:
<app-comp [item]="item"></app-comp>
What I want to achieve is: 我想要实现的是:
<div app-comp [item]="item"></div>
A component can have an attribute selector, in your component definition use: 组件可以具有属性选择器,在组件定义中使用:
@Component({
selector: 'div[app-comp]'
...
Or simply 或者简单地
@Component({
selector: '[app-comp]'
...
If you want to use it both as a tag or as an attribute, use: 如果要同时将其用作标签或属性,请使用:
@Component({
selector: 'app-comp, [app-comp]'
...
The documentation reference for this is here: https://angular.io/api/core/Directive#selector 此处的文档参考: https : //angular.io/api/core/Directive#selector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.