组件渲染后如何从指令调用函数?
我有组件:
export class Component {
ngAfterContentInit() {
// How can i call functionFromDirective()?
}
}
我想调用这个函数:
export class Directive {
functionFromDirective() {
//something hapenns
}
我怎样才能做到这一点?
您可以使用以下命令从组件的模板中检索指令ViewChild
像这样:
@Directive({
...,
selector: '[directive]',
})
export class DirectiveClass {
method() {}
}
在您的组件中:
import { Component, ViewChild } from '@angular/core'
import { DirectiveClass } from './path-to-directive'
@Component({
...,
template: '<node directive></node>'
})
export class ComponentClass {
@ViewChild(DirectiveClass) directive = null
ngAfterContentInit() {
// How can i call functionFromDirective()?
this.directive.method()
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)