I'm trying to call a service from a function but i'm getting this error in the console:
Cannot read property 'TestMethod' of undefined
This is my code:
app.component.ts:
constructor(public _service: BackendFileCodeService){ } public editor; EditorCreated(quill) { const toolbar = quill.getModule('toolbar'); this.editor = quill; // console.log(quill) toolbar.addHandler('image', this.imageHandler); } imageHandler(value) { // document.querySelector('input.ql-image[type=file]').addEventListener('click', () => { // console.log('Hello'); // }); const ImageInput = document.createElement('input'); ImageInput.setAttribute('type', 'file'); ImageInput.setAttribute('accept', 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon'); ImageInput.classList.add('ql-image'); ImageInput.click(); ImageInput.addEventListener('change', () => { const file = ImageInput.files[0]; if (ImageInput.files != null && ImageInput.files[0] != null) { this._service.TestMethod('Hello'); } }); }
BackendFileCodeService:
import { Injectable } from '@angular/core'; @Injectable() export class BackendFileCodeService { constructor() { } TestMethod(test){ return test; } }
I'm trying to call the service inside the function called imageHandler specifically in the
ImageInput.addEventListener
but i'm getting the error mentioned up, i tried to call the service from outside the imageHandler function and every this works as expected.
Note: the service is console log 'hello' as test.