繁体   English   中英

Angular:带有指令的模板引用变量

[英]Angular: template reference variable with directives

我在Angular文档中读到,我们可以在custom component上使用template reference variable ,并且可以访问该组件的公共方法和属性。

我在带有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>

是否可以将template reference variable与指令一起使用以访问该指令的属性?

是的,您可以使用指令元数据声明的exportAs属性

@Directive({
  selector: '[my-directive]',
  exportAs: "myDirective"
})
export class MyDirective {
  name: string = "Hello World";
  constructor() { }
}

然后在您的html中,您应该按以下方式访问

<div my-directive #t=myDirective>
  {{ t.name }}
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM