简体   繁体   English

angular4中的事件绑定

[英]Event Binding in angular4

I want to call methods on click event bind click event on ngOnInit() using angular4. 我想使用angular4调用click事件上的方法,将ngOnInit()上的click事件绑定。 My requirement is to bind click event on span which i have done it but when i call my other method using if conditions then gives me the error this.getSellingChats is not a function in console. 我的要求是在我已经完成的跨度上绑定click事件,但是当我使用if条件调用我的其他方法时,就会给我错误error.getSellingChats不是控制台中的函数。 here is my code 这是我的代码

ngOnInit() { 

this.scrollToBottom();
this.getallChats();
this.checkConnection();
setTimeout(() => {
    this.bindClickevents();    
}, 3000);

}

bindClickevents(){
$('tabset ul li a').find('span').bind('click',function(event){

    var _tab = $(this).text();
    if(_tab == 'ALL'){
        this.getallChats();
    }
    else if(_tab == 'SELLING'){
        this.getSellingChats();

    }
    else if(_tab == 'BUYING'){
        this.getBuyingChats();
    }
    else if(_tab == 'BLOCKED'){
        this.getBlockedChats();
    }
});

} }

Basically what i want to achieve is bind click event on span and then user click on different tabs methods called according to if conditions. 基本上,我要实现的是在span上绑定click事件,然后用户根据if条件调用不同的选项卡方法。 I don't want to call the following methods in ngOnInit() method. 我不想在ngOnInit()方法中调用以下方法。

    this.getSellingChats();
    this.getBuyingChats();
    this.getBlockedChats();

Please provide me solution. 请提供给我解决方案。 Will be highly appreciated. 将不胜感激。

this is not the reference of your component inside the find method. this不是find方法中组件的引用。 Try it like following. 尝试以下操作。

    bindClickevents(){
    const comp = this;
    $('tabset ul li a').find('span').bind('click',function(event){

        var _tab = $(this).text();
        if(_tab == 'ALL'){
            comp.getallChats();
        }
        else if(_tab == 'SELLING'){
            comp.getSellingChats();

        }
        else if(_tab == 'BUYING'){
            this.getBuyingChats();
        }
        else if(_tab == 'BLOCKED'){
            comp.getBlockedChats();
        }
    });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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