我试图通过像这样的viewchild获取指令来调用一个公共函数,该函数位于我的组件指令中
- @ViewChild('myDirective') myDirective;
- ....
- myDirective.nativeElement.myFunction();
DEMO – How to call Directive’s function from Component – 检查浏览器
安慰
安慰
1)我希望你导入myDirective指令.
- import {myDirective} from './Directive';
2)
- @ViewChild(myDirective) vc:myDirective; ///<<<@@@removed '' from ('myDirective')
3)
- ngAfterViewInit(){
- this.vc.myFunction(); ///<<@@@ no need to use nativeElement
- }
4)或某些按钮的点击事件
- click(){
- this.vc.myFunction();
- }