如何在组件渲染后从指令中调用函数?
我有组件:
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()
}
}
关于angular - 组件渲染后如何从指令中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40549894/