How can I bind my click function to my button in KnockoutJS?

时间:2017-06-14 10:26:33

标签: javascript html knockout.js telegram

In the code below you can see that I'm trying to add a click-event on my button which I'm creating dynamically in JavaScript with data-bind:"click: replay". But it seems like the code isn't executing the click function. However when I create manually a button in HTML, the click event do work.

var GeneralTelegram = function(){

    var self = this;

    app_socket_handler = function(data) {
        switch (data.action) {
            case "messageIncomming":
    var message = data.message.text;
    var firstname = data.message.from.first_name;
    var lastname = data.message.from.last_name;
    var name = firstname + " " + lastname;
    $('#messages').prepend('<div id="message"><p>'+message+'</p><p id="name">'+name+'</p><a href="#" class="button" data-bind="click: replay">click</a></div>'); 

        self.replay = function(){
            console.log('test');
        }

    break;

    default:
        }

        return;

}

}

0 个答案:

没有答案