假设我有以下标记:
<my-comp myDirective></my-comp>
有没有办法访问组件实例from the directive?
更具体地说,我希望能够从MyDirective
,最好是without adding anything to the HTML above访问MyComponent
的属性和方法.
假设我有以下标记:
<my-comp myDirective></my-comp>
有没有办法访问组件实例from the directive?
更具体地说,我希望能够从MyDirective
,最好是without adding anything to the HTML above访问MyComponent
的属性和方法.
你可以直接注射
class MyDirective {
constructor(private host:MyComponent) {}
一个严重的限制是,您需要提前知道组件的类型.
另请参阅https://github.com/angular/angular/issues/8277
对于事先不知道类型的情况,它还提供了一些解决方法.