Angular 调用函数

Angular 调用函数,angular,Angular,在我的angular项目中,我有以下内容: main.html: <my_project [Number]="ID"></my_project> export class my_project { @Input() Number: Array<any>; ... my_function(id){ console.log("ID number: " + id); }; } 我的项目。ts: &l

在我的angular项目中,我有以下内容:

main.html:

   <my_project [Number]="ID"></my_project> 
export class my_project {
   @Input() Number: Array<any>; 

   ...

   my_function(id){
      console.log("ID number: " +  id);
   };

}

我的项目。ts:

   <my_project [Number]="ID"></my_project> 
export class my_project {
   @Input() Number: Array<any>; 

   ...

   my_function(id){
      console.log("ID number: " +  id);
   };

}
导出类my_项目{
@Input()编号:数组;
...
my_函数(id){
控制台日志(“ID号:+ID”);
};
}
我知道如何将数据传递给另一个指令。我想知道是否有一种方法可以直接从“main.html”或“main.ts”调用函数,如下所示:

<my_project [my_function]></my_project>

任何帮助都将不胜感激


谢谢

多次意味着什么。 1) 以多种方式从同一组件, 2) 在路线负载上,您需要呼叫。 3) 从您想要交互的不同组件

在每种情况下都有选项,请具体说明。

尝试如下:

<my_project [my_function]="[this, 'this.name = 1; this.sampleFun()']"></my_project>
模块.ts

import { Component, OnInit, Directive } from '@angular/core';

@Directive({
    selector: '[my_function]',
    inputs: ['my_function']
})

export class NgInitDirective {
    my_function;

    ngOnChanges() {
        if (this.my_function) {
            let iife = function(str) { return eval(str); }.call(this.my_function[0], this.my_function[1]);
        }
    }
}

@Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

    constructor() { }

    ngOnInit() { }

    sampleFun() {
        console.log('hello word');
    }
}
import {
    AppComponent,
    NgInitDirective
} from './';

@NgModule({
    declarations: [
        AppComponent,
        NgInitDirective
    ],
    entryComponents: [
        AppComponent
    ],
    providers: [],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule { }

为什么不把你的方法放在ngOnInit中,它会在加载时自动启动?我应该澄清一下,它被多次调用,而ngOnInit最初只启动一次。由于我需要多次调用“”,因此我正在寻找调用函数的方法。您需要澄清何时希望调用此函数。否则,我们不会更改帮助。您可以在输入中使用setter,在setter调用该函数时,这可能有助于您在设置值时多次调用它