为什么变量未定义,即使它被定义了?

时间:2017-07-12 17:42:11

标签: typescript

我正在使用Ionic框架为移动设备创建应用。所以你可以看到,我创建了加载栏,我想在一切完成后解雇它。但我也想改变我的布尔。但是控制台说它们是未定义的。为什么?

export class AfterEditPage {

    public loaded_image = base64Image;
    public showMainContent: boolean = true; // I am interacting with this one
    public showAdditionalContent: boolean = false; // and this one
    public processing_Result: string = "";

    constructor(public loadingctrl: LoadingController, public alertctrl: AlertController, public navCtrl: NavController, public navParams: NavParams) {

    }

    recognizeImage() {

    let resultText;
    console.log(resultText);
    let loader = this.loadingctrl.create({
            content: 'Processing...'
    });

    console.log(this.processing_Result);
    loader.present().then(() => {
            Tesseract.recognize(this.loaded_image)
            .progress(function  (p) {})
            .then(function (result) {
                    resultText = result.text; console.log(resultText);
                    loader.dismiss().then(() => {
                            this.showMainContent = !this.showMainContent; // undefined
                    this.showAdditionalContent = !this.showAdditionalContent; // undefined
                    });
                });
              });
         }
    }

1 个答案:

答案 0 :(得分:0)

您需要使用箭头功能,因此您需要将代码更改为

 loader.present().then(() => {
            Tesseract.recognize(this.loaded_image)
            .progress((p) => {})
            .then((result) => {
                    resultText = result.text; console.log(resultText);
                    loader.dismiss().then(() => {
                            this.showMainContent = !this.showMainContent; 
                    this.showAdditionalContent = !this.showAdditionalContent;
                    });
                });
              });
         }
    }