[英]Angular: template reference variable with directives
I read in the Angular docs that we can use a template reference variable
on a custom component
and we can get access to the public methods and properties of this component. 我在Angular文档中读到,我们可以在
custom component
上使用template reference variable
,并且可以访问该组件的公共方法和属性。
I tried the same on an element with a directive
and it does not work. 我在带有
directive
的元素上尝试了相同的操作,但它不起作用。
<table #myTable my-directive>
<tr>
<th>Name</th>
</tr>
<tr>
<!--test value is a prop of my directive -->
<td>Some text: {{ myTable.testValue }}</td>
</tr>
</table>
Is it possible to use a template reference variable
with a directive to get access to this directive's properties? 是否可以将
template reference variable
与指令一起使用以访问该指令的属性?
Yes, you can use exportAs property of Directive metadata declartion 是的,您可以使用指令元数据声明的exportAs属性
@Directive({
selector: '[my-directive]',
exportAs: "myDirective"
})
export class MyDirective {
name: string = "Hello World";
constructor() { }
}
Then in your html you should access as follows 然后在您的html中,您应该按以下方式访问
<div my-directive #t=myDirective>
{{ t.name }}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.