2017-08-31 76 views
1

我有2個函數,我試圖讓其他函數的返回值繼續,但返回值始終未定義。如何獲得離子2中的返回值?

這是我的代碼返回值。

export class HomePage { 

    variables={ 
    a:1, 
    b:2, 
    c:3, 
    } 

    constructor(public navCtrl: NavController) { 
    this.function1(); 
    } 

    function1(){ 
    if(function2()){ 
     alert("true"); 
    }else{ 
     alert("false"); 
    } 
    } 

    function2(){ 
    if(this.variables.a + this.variables.b >= this.variables.c){ 
     return true; 
    }else{ 
     return false; 
    } 
    } 
} 
+0

是一個組件的一部分?你能告訴我們整個組件代碼嗎? – sebaferreras

+1

@sebaferreras,我用我的完整代碼保存了我的問題編輯 –

+0

謝謝,請讓我知道如果答案解決了問題:) – sebaferreras

回答

1

既然你聲明作爲組件的一部分,你需要使用this關鍵字來執行他們兩個功能:

function1(){ 
    if(this.function2()){ // <--- like this! 
     alert("true"); 
    }else{ 
     alert("false"); 
    } 
    } 
+0

謝謝,是的錯誤解決,但警報總是顯示「錯誤」。當我檢查console.log(this.function2()); 。那麼它顯示我「undefined」 –

+0

我已經使用完全相同的代碼(使用'this'關鍵字作爲函數)創建了[this demo plunker](http://plnkr.co/edit/dEBXpx7z0Na1KpetjubY?p=preview)似乎工作正常。你可以看看嗎? – sebaferreras

+1

我很抱歉。當我編輯我的代碼時,我犯了錯誤。它的工作很好..非常感謝你 –