简体   繁体   中英

Template Strings in Angular: How to bind a function?

I wonder if it is possible to bind a function to a html Template via the template-string notation.

So if i do this

 let dynTamp = `<div> ${device.name} </div>`;

it works perfectly well, but this

let dynTamp = `<button (click)="${someFunction}"></button>`;

does not bind the function to the click event. Am I missing something here?

The Background: I am working with LeafletJs and dynamically create markers on a map, whose popovers should contain buttons:

this.devices.forEach(device => {

        L.marker([Number(device.map_x), Number(device.map_y)], {icon: this.icon})
          .bindPopup(`<h2> ${device.name}</h2>
                  <button (click)="${doThis}"></button>
                  <button (click)="${doThat}"></button>`)
          .addTo(this.map)           
    }

This should be the correct syntax :

let dynTamp = `<button (click)="someFunction()"></button>`;

I share an example of how to use a template string : View Example

please replace with this

let dynTamp = `<button (click)="someFunction()"></button>`;

instead of

let dynTamp = `<button (click)="${someFunction}"></button>`;

An angular expression is not a template string. It's an Angular expression. The syntax to call someFunction() when a button is clicked is (click)="someFunction() ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM