Passing a function into a Handlebars template

后端 未结 2 639
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 06:04

I\'m using (or at least starting with) HandlebarsJS for the html templates but I might have hit a dead end. What I want is to pass a function to the template, e.g.



        
2条回答
  •  难免孤独
    2020-12-16 06:23

    You can also make global defined callback function, and pass function calling string in the onclick value in the template.

    Note the parenthesis with the tmpCallback for func value in data object.

    var tmpCallback = function () {
        alert('hello');
    }
    
    var data = {
      "text": "Click here",
      "func": "tmpCallback()"
    };
    
    $('body').append(template(data));
    

    This is just a hack for quick workaround, and I think answer by @BFil may be a better one.

提交回复
热议问题