Angular 2 - how to access the directive and call a member function in it

I have such a directive -

@Directive({ selector: 'someDirective' }) export class SomeDirective{ constructor() {} render = function() {} } 

and then I import the directive

 import {SomeDirective} from '../../someDirective'; @Page({ templateUrl: '....tem.html', directives: [SomeDirective] }) export class SomeComponent { constructor(){} ngAfterViewInit(){//want to call the render function in the directive } 

In ngAfterViewInit, I want to call the render function in a directive. How to do it?

+8
angular ionic2
source share
1 answer

This is the old way

 @Directive({ selector: 'someDirective' }) export class SomeDirective{ constructor() {} render() { console.log('hi from directive'); } } import {Component,ViewChild} from 'angular2/core'; import {SomeDirective} from '../../someDirective'; @Component({ templateUrl: '....tem.html', directives: [SomeDirective] }) export class SomeComponent { @ViewChild(SomeDirective) vc:SomeDirective; constructor(){} ngAfterViewInit(){ this.vc.render(); } } 

for a newer version of Angular2, follow the answers given here

Directive call function

+12
source share

All Articles