从HTML调用打字稿文件中的函数。

Rok*_*545 6 html javascript twitter-bootstrap angularjs angular

我是HTML和Angular 2的新手。我目前正在尝试弄清楚如何从HTML文件中调用Typescript文件中的函数。

简而言之,Typescript文件(home.ts)函数如下所示:

getConfigurations(sensorName: string) {
    console.log('Home:getConfigurations entered...');

    return 'sensor1';
}
Run Code Online (Sandbox Code Playgroud)

在我的HTML文件(home.html)中,我想用字符串参数调用“ getConfigurations”(由于我仍在测试中,因此“ getConfigurations()”现在返回“ sensor1”)。因此,在HTML中,如何使用参数调用getConfigurations方法(为简单起见,如何在简单的div面板中调用该方法)?(我已经有了要传入名为“ selectedSensor”的变量的值)。

Par*_*ain 7

Angular提供的功能是通过使用事件绑定功能,您可以调用ts文件中存在的函数,也可以使用angular的插值语法来调用函数,如下所示:-

<button (click)='getConfigurations("parameter")'>Button Click</button>
Run Code Online (Sandbox Code Playgroud)

或类似的东西

{{getConfigurations('parameter')}}  
Run Code Online (Sandbox Code Playgroud)

有关与angular2中的事件绑定相关的更多信息,请参见此处

工作实例 Working Plunker