angulartesseract.js

Tesseract callback with Angular - 'this' is undefined


I am trying to get work Tesseract with Angular 2.

Tesseract.recognize(file)
    .progress(function (p) { console.log('progress', p) })
    .then(function (result) {
      this.resultText = result.text;
    })
}

The problem is on row: this.resultText = result.text;. I am getting error 'Cannot read property 'resultText' of undefined'.

I am not sure why this is undefined.


Solution

  • Use the arrow function syntax to bind this correctly in the callback

    Tesseract.recognize(file)
        .progress((p) => { console.log('progress', p) })
        .then((result) => {
          this.resultText = result.text;
        })
    }