Angular 2-如何访问指令并调用其中的成员函数
我有这样的指示-Angular 2-如何访问指令并调用其中的成员函数,angular,ionic2,Angular,Ionic2,我有这样的指示- @Directive({ selector: 'someDirective' }) export class SomeDirective{ constructor() {} render = function() {} } 然后我导入指令 import {SomeDirective} from '../../someDirective'; @Page({ templateUrl: '....tem.html',
@Directive({
selector: 'someDirective'
})
export class SomeDirective{
constructor() {}
render = function() {}
}
然后我导入指令
import {SomeDirective} from '../../someDirective';
@Page({
templateUrl: '....tem.html',
directives: [SomeDirective]
})
export class SomeComponent {
constructor(){}
ngAfterViewInit(){//want to call the render function in the directive
}
在ngAfterViewInit中,我想调用指令中的render函数。
怎么做?这是老办法,
对于Angular2的更新版本,请遵循此处给出的答案
谢谢你的回答,正如你所说,我已经试过了,但是我得到了错误-语法错误-@ViewChild(SomeDirective)vc:SomeDirective;在该行的冒号“:”处。我用的是离子2。我尝试了两种导入方式-->从'angular2/core'导入{ViewChild};并从“离子角度”导入{ViewChild};哎呀!我不喜欢你的框架。所以在这种情况下我帮不了你。但我确信这就是ViewChild的用法。嘿,你完全正确,只是我在用ES6编写代码。因此,在TS中编写代码并进行传输,一切正常。如果您能告诉我如何在ES6/ES5中编写相同的代码,那就太好了。谢谢。很难从我这边告诉你,因为我也只是用TS写东西。但我相信,如果你想用ES6编写它,这并不难。
@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();
}
}