我试图通过像这样的viewchild获取指令来调用一个公共函数,该函数位于我的组件指令中
@ViewChild('myDirective') myDirective;
....
myDirective.nativeElement.myFunction();
Run Code Online (Sandbox Code Playgroud)
但是我得到myFunction不存在的错误.有没有办法调用一个内部指令的函数?
mic*_*yks 22
DEMO - 如何从Component调用Directive的功能 - 检查浏览器的控制台
import {myDirective} from './Directive';
Run Code Online (Sandbox Code Playgroud)
2)
@ViewChild(myDirective) vc:myDirective; ///<<<@@@removed '' from ('myDirective')
Run Code Online (Sandbox Code Playgroud)
3)
ngAfterViewInit(){
this.vc.myFunction(); ///<<@@@ no need to use nativeElement
}
Run Code Online (Sandbox Code Playgroud)
4) 或某些按钮的点击事件
click(){
this.vc.myFunction();
}
Run Code Online (Sandbox Code Playgroud)
使用@ContentChild().指令没有视图.
通话this.myDirective.nativeElement.myFunction()中ngAfterContentChecked(),确保this.myDirective...已初始化.
| 归档时间: |
|
| 查看次数: |
9505 次 |
| 最近记录: |