簡體   English   中英

angular4中的事件綁定

[英]Event Binding in angular4

我想使用angular4調用click事件上的方法,將ngOnInit()上的click事件綁定。 我的要求是在我已經完成的跨度上綁定click事件,但是當我使用if條件調用我的其他方法時,就會給我錯誤error.getSellingChats不是控制台中的函數。 這是我的代碼

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();
    }
});

}

基本上,我要實現的是在span上綁定click事件,然后用戶根據if條件調用不同的選項卡方法。 我不想在ngOnInit()方法中調用以下方法。

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

請提供給我解決方案。 將不勝感激。

this不是find方法中組件的引用。 嘗試以下操作。

    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