最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling a function in a typescript file from HTML. - Stack Overflow

programmeradmin2浏览0评论

I'm new to HTML and Angular 2. I'm currently trying to figure out how to call a function that's inside a Typescript file from an HTML file.

Stripped down, the Typescript file (home.ts) function looks like this:

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

    return 'sensor1';
}

In my HTML file (home.html), I would like to call 'getConfigurations' with a string parameter (right now 'getConfigurations()' returns 'sensor1' since I'm still testing things out). So, in HTML, how do I go about calling my getConfigurations method with a parameter (for simplicity - how can I call the method inside a simple div panel)? (I already have the value I want to pass in a variable called 'selectedSensor').

I'm new to HTML and Angular 2. I'm currently trying to figure out how to call a function that's inside a Typescript file from an HTML file.

Stripped down, the Typescript file (home.ts) function looks like this:

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

    return 'sensor1';
}

In my HTML file (home.html), I would like to call 'getConfigurations' with a string parameter (right now 'getConfigurations()' returns 'sensor1' since I'm still testing things out). So, in HTML, how do I go about calling my getConfigurations method with a parameter (for simplicity - how can I call the method inside a simple div panel)? (I already have the value I want to pass in a variable called 'selectedSensor').

Share Improve this question asked May 16, 2016 at 23:16 Roka545Roka545 3,62622 gold badges69 silver badges111 bronze badges 1
  • 2 How is your angular app structured? You need to arrange things so the function is in scope and then you can just call it with {{getConfigurations(param)}} but people can't give you proper help without more info. – Paarth Commented May 16, 2016 at 23:19
Add a comment  | 

1 Answer 1

Reset to default 13

There is feature provided by angular is events binding by using you are able to call function whihc is exist in your ts file also you can use interpolation syntax of angular to call function like this : -

<button (click)='getConfigurations("parameter")'>Button Click</button>

or something like this

{{getConfigurations('parameter')}}  

for more info related to event binding in angular2 see here

working example Working Plunker

发布评论

评论列表(0)

  1. 暂无评论