Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何获取离子2中的返回值?_Angular_Typescript_Ionic2_Ionic3 - Fatal编程技术网

Angular 如何获取离子2中的返回值?

Angular 如何获取离子2中的返回值?,angular,typescript,ionic2,ionic3,Angular,Typescript,Ionic2,Ionic3,我有两个函数,我试图让其他函数的返回值继续,但返回值始终未定义 这是我返回值的代码 export class HomePage { variables={ a:1, b:2, c:3, } constructor(public navCtrl: NavController) { this.function1(); } function1(){ if(function2()){ alert("true");

我有两个函数,我试图让其他函数的返回值继续,但返回值始终未定义

这是我返回值的代码

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

由于您将这两个函数声明为组件的一部分,因此需要使用
this
关键字来执行它们:

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

如果(this.function2()){//这是组件的一部分吗?你能给我们看一下整个组件代码吗?@sebafereras,我用完整的代码保存了对问题的编辑谢谢,请告诉我答案是否解决了问题:)谢谢,是的,错误解决了,但警报总是显示“false”。当我检查console.log(this.function2())时;然后显示“undefined”(未定义),我用完全相同的代码(使用函数的
this
关键字)创建的,并且似乎工作正常。你能看一下吗?我很抱歉。我在编辑代码时出错了。工作正常。非常感谢你,听到这个消息很高兴:)