Steps for utilizing a function from the parent component to initialize TinyMCE

I have implemented an uploading function in my parent component. As I set up tinymce, I connected the [init] property of my component to the loadConfig() function.

<editor [(ngModel)]="data" [init]="loadConfig()"></editor>

The loadConfig function includes the images_upload_handler as follows:

images_upload_handler: function (blobInfo, success, failure) {
        const code = this.upload(blobInfo);
        if (code !== 0) {
          success(code);
        } else {
          failure(code);
        }
      }

However, when attempting to upload something, I receive an error stating that 'upload' is undefined. This issue seems to stem from a change in scope for 'this'.

My attempt to address this involved listening for the (onInit) event and updating the function reference with the following code:

event.editor.settings.images_upload_handler = this.Upload;

Yet now I am facing errors mentioning that 'this.apiService' is undefined, indicating that the correct scope is still not being used.

I considered utilizing XMLHttpRequest(), but would need security tokens to proceed. Instead, I prefer to leverage the apiService I have created. How can I utilize the images_upload_handler without losing access to my apiService's scope?

Answer №1

After some troubleshooting, the solution was found by converting the function into an Arrow function. Unlike regular functions, arrow functions do not have their own bindings to this or super.

images_upload_handler: (blobInfo, success, failure) => {
        const response = this.upload(blobInfo);
        if (response !== 0) {
          success(response);
        } else {
          failure(response);
        }
      }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The browser automatically adds a backslash escape character to a JavaScript object

When attempting to send an MQTT message to a topic from my angular app, the message needs to be in a specific syntax: { "Message": "hello" //the space after : is mandatory } However, upon sending the message in the correct format, the browser aut ...

Unable to subscribe due to the return value being an AnonymousSubject rather than an Observable

I am facing an issue in Angular 4 where I am attempting to retrieve details from a specific user when clicking on the 'user link profile'. However, I am unable to subscribe to my function because the return value of switchMap is AnonymousSubject ...

Optimal method for efficiently caching data when toggling between list view and detail view within Angular 12

In my Angular App, I have implemented two components - a list view and a details view. Users can switch between these components, both of which utilize multiple async pipes. To minimize the number of http requests to the backend, I decided to cache data u ...

Utilize the class or interface method's data type

In the context of a child component calling a callback provided by its parent, this situation is commonly seen in AngularJS. As I am utilizing TypeScript, I aim to implement strong typing for the callback in the child component. Here is the initial stat ...

What are the best practices for utilizing fetch() to retrieve data from a web API effectively?

Is there a way to store stock data in stockData and display it in the console upon form submission? Upon submitting the form, I only receive undefined. I suspect this may be due to a delay in fetching data from the API (but not certain). How can I resol ...

When working with Angular 8, you may encounter an issue where the AWSIoTProvider from aws-amplify

After referencing the official link document here, I found that my code works fine with ng serve. However, after building it and trying to access the page, I encountered an error stating "AWSIoTProvider is not a constructor ". Despite searching for a sol ...

Discover the outcome of clicking on an object (mock tests)

I am just starting out with React and I'm unsure about when to use mocking. For instance, within the 'ListItem' component, there is a 'click me' button that reveals a dropdown for 'cameras'. Should I focus on testing what ...

Issue with closing the active modal in Angular TypeScript

Can you help me fix the issue with closing the modal window? I have written a function, but the button does not respond when clicked. Close Button: <button type="submit" (click)="activeModal.close()" ...

Angular's custom reactive form validator fails to function as intended

Struggling to incorporate a customized angular validator to check for date ranges. The validator is functioning correctly and throwing a validation error. However, the issue lies in the fact that nothing is being displayed on the client side - there are n ...

Child component received an incorrect input value

Utilizing both the "YearComponent" and "StatPeriod" components has presented some challenges for me. Within my YearComponent, I invoke StatPeriod as follows: <app-save-stat-period [dateBegin]="dateBegin" [dateEnd]="dateEnd" byMonth bestNAverage></ ...

What is the process for loading the chosen option JSON object from an array when a button is clicked?

I have a TypeScript file containing JSON objects that can be selected as options from a dropdown list. I am looking for guidance on how to load/instantiate the selected JSON object when the user clicks a button to proceed. Specifically, I would like to le ...

Methods for verifying an empty array element in TypeScript

How can I determine if an element in an array is empty? Currently, it returns false, but I need to know if the element is blank. The array element may contain spaces. Code let TestNumber= 'DATA- - -' let arrStr =this.TestNumber.split(/[-]/) ...

Alternative image loading in a figure element

I'm currently in the process of putting together an image gallery using Angular 4.3.0, where images are displayed as background images within figure elements rather than img tags. The images are initially resized to smaller dimensions before being use ...

Utilizing Typescript with Angular 2 to efficiently convert JSON data into objects within HTTP requests

I am dealing with a file called location.json, which contains JSON data structured like this: { "locations": [ { "id": 1, "places": [ { "id": 1, "city": "A ...

Executing the Angular 2 foreach loop before the array is modified by another function

Currently, I am facing some difficulties with an array that requires alteration and re-use within a foreach loop. Below is a snippet of the code: this.selectedDepartementen.forEach(element => { this.DepID = element.ID; if (this.USERSDepIDs. ...

Ways to adjust the size of a Primeng autocomplete with multiple selections?

I'm new to Angular and I'm attempting to adjust the width of a primeng auto complete component in order to fill the column of a table. I've already experimented with using style="width: 100%" but that approach hasn't yielded any result ...

The act of updating Angular 2 to Angular 4 has resulted in an issue where the 'AnimationDriver' member is not being exported

After upgrading my Angular 2 to Angular 4, I encountered an error message when running my project: The module 'node_modules/@angular/platform-browser/platform-browser' does not have the exported member 'AnimationDriver'. ...

Middleware fails to execute on routing in Nextjs 13.4 application

Something's not quite right. I can't seem to get my middleware to run... Here's the code I'm using: export const config = { matcher: '/api/:function*', }; I specified this config so that it would run only when there's ...

Indicate a specific type for the Express response

Is there a method to define a specific type for the request object in Express? I was hoping to customize the request object with my own type. One approach I considered was extending the router type to achieve this. Alternatively, is there a way to refactor ...

Angular 5: Issue with Module Resolution: Unable to locate '@angular/forms/src/validators'

Struggling to develop a unique validator using a directive, but encountering the following error: ERROR in ./src/app/CustomValidators/white-space-validator.directive.ts Module not found: Error: Can't resolve '@angular/forms/src/validators' ...