2017-08-31 77 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

我很抱歉。当我编辑我的代码时,我犯了错误。它的工作很好..非常感谢你 –