angular – 指令中的调用函数

前端之家收集整理的这篇文章主要介绍了angular – 指令中的调用函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过像这样的viewchild获取指令来调用一个公共函数,该函数位于我的组件指令中
  1. @ViewChild('myDirective') myDirective;
  2. ....
  3. myDirective.nativeElement.myFunction();

但是我得到myFunction不存在的错误.
有没有办法调用一个iniside指令的函数

DEMO – How to call Directive’s function from Component – 检查浏览器
安慰

1)我希望你导入myDirective指令.

  1. import {myDirective} from './Directive';

2)

  1. @ViewChild(myDirective) vc:myDirective; ///<<<@@@removed '' from ('myDirective')

3)

  1. ngAfterViewInit(){
  2. this.vc.myFunction(); ///<<@@@ no need to use nativeElement
  3. }

4)或某些按钮的点击事件

  1. click(){
  2. this.vc.myFunction();
  3. }

猜你在找的Angularjs相关文章