Angular Material modal not opening with QuillJS image handler

I would like to customize Quill's image button behavior by implementing a unique function that triggers an Angular Material modal.

Component html

<form>
    <mat-form-field appearance="outline">
        <input matInput placeholder="Titulo" type="text">
    </mat-form-field>

    <div class="editor-wrapper">
        <quill-editor required="true" id="editor" (onEditorCreated)="getEditorInstance($event)">
            <div quill-editor-toolbar>
                    <span class="ql-formats">
                            <select class="ql-size"></select>
                      <select class="ql-font"></select>
                    </span>
                    <span class="ql-formats">
                      <button class="ql-bold"></button>
                      <button class="ql-italic"></button>
                      <button class="ql-underline"></button>
                    </span>
                <span class="ql-formats">
                  <select class="ql-color"></select>
                  <select class="ql-background"></select>
                </span>
                <span class="ql-formats">
                  <button class="ql-list" value="ordered"></button>
                  <button class="ql-list" value="bullet"></button>
                  <select class="ql-align"></select>
                </span>
                <span class="ql-formats">
                  <button class="ql-link"></button>
                  <button class="ql-image"></button>
                  <button class="ql-video"></button>
                </span>
            </div>
        </quill-editor>
    </div>
</form>

Component ts


import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material';

import { UploadImageComponent } from './../modals/upload-image/upload-image.component';

@Component({
    selector: 'app-write',
    templateUrl: './write.component.html',
    styleUrls: ['./write.component.scss']
})
export class WriteComponent implements OnInit {

    constructor(private dialog: MatDialog) { }

    ngOnInit() { }

    getEditorInstance(editorInstance: any) {
        const toolbar = editorInstance.getModule('toolbar');
        toolbar.addHandler('image', this.addImage);
    }

    public addImage() {
        this.dialog.open(UploadImageComponent).afterClosed().subscribe(result => {
            console.log('Closed');
        });
    }
}

Upon clicking the image button, I encounter the following error message:

ERROR TypeError: Cannot read property 'open' of undefined

It seems that when passing the function to the handler, it loses access to the dialog variable in the WriteComponent class.

Any suggestions?

Answer №1

Perhaps the 'this' you are referring to is incorrect, try adjusting the context using bind(this)

toolbar.addHandler('image', this.addImage.bind(this));

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

Using Typescript: storing a reference to an interface within a variable

During the implementation of client-side server communication, I encountered a particular challenge: How can I store a reference to an interface in a variable? I have defined an interface that contains all the details related to a specific RESTful backend ...

Unable to determine the data type of the property within the table object

Is it feasible to retrieve the type of object property when that object is nested within a table structure? Take a look at this playground. function table<ROW extends object, K extends Extract<keyof ROW, string>>({ columns, data, }: { col ...

Unable to locate the identifier 'BrowserWindow'

In the providers folder of electron.service.ts, I have the following code: import { Injectable } from '@angular/core'; // If you import a module but never use any of the imported values other than as TypeScript types, // the resulting javascrip ...

What are the most effective applications for utilizing an Observable Data Service?

Within my application setup, I have integrated a data service at the core level. The majority of functions within my app involve actions taken on the data model, to which components react. My goal is for components to be able to subscribe to the data ser ...

Tips to prevent scrolling in md-content or set a minimum height for mobile devices

Utilizing Angular Material's md-content directives, I am working on creating a section that adjusts in height to fill the available vertical space while allowing its content to scroll. However, a problem arises when viewing the page on a small screen ...

What is the best way to connect input values with ngFor and ngModel?

I am facing an issue with binding input values to a component in Angular. I have used ngFor on multiple inputs, but the input fields are not showing up, so I am unable to push the data to subQuestionsAnswertext. Here is the code snippet from app.component ...

Steps for subscribing to a component variable in Angular 2

I've set up a header for my server URL in one of the components, and I want to use this header in all components whenever there is a server call. However, I am having trouble subscribing to this header. Can someone please assist me? Interface compone ...

Definition of type instantiation in TypeScript

When utilizing the mynew function with a specified array of classes, I am encountering an error related to defining unknown. How can I modify this definition in order to eliminate the error? export interface Type<T> extends Function { new (...arg ...

Tips for leveraging the functions service in Next.js for better code reusability

I am just starting to learn Next.js and I have a preference for organizing my API functions in a separate folder called services. I attempted to implement some code based on this topic but unfortunately, it did not work as expected. It seems like my api fu ...

Exploring Angular: Effective ways to test RadioButtons

Have you tried using MatButtonHarness from Angular Material to test regular HTML buttons? It allows you to 'getText' and 'click' with ease. However, I couldn't find a similar option for testing RadioButtons like MatRadioHarness. A ...

Caution: Be cautious when attempting to set up the angular2 dropdown multiselect library

After attempting to set up the angular 2 drop down multi select library with the command: npm install angular-2-dropdown-multiselect --save-dev An error message displays, stating: "[email protected] requires a peer of tslib@^1.6.1 but none was installed. ...

Retrieve the text value from a changing dropdown menu in Selenium using Java

Need help selecting a value by text from a dynamic non-select dropdown. I've been looking into this and came across the following code snippet: WebElement element = driver.findElement(ByMapper.getBy(dropdown)); element.click(); List<WebElement> ...

Why isn't my Next.js middleware working properly with TypeScript?

My issue arises from the fact that, despite following the documentation, the middleware in Next.js is not functioning as I anticipated. I experimented with what I thought was the simplest middleware possible. I expected that when navigating to /, a conso ...

Starting the process of configuring Angular 5 with Express using TypeScript

Hi there! I am looking to create a fresh application using angular 5 and express (typescript for express as well). Do you have any helpful guides or tips that could assist me in reaching my objective? Appreciate all the help, Giuseppe ...

Command to set up webpack configuration for watching changes in Angular 2 CLI: ng build --watch

Can I customize the configuration for my ng build --watch command in order to efficiently bundle and minify files on disk each time a modification is made? ...

Retrieve the Typescript data type as a variable

I have the following components: type TestComponentProps = { title: string; } const TestComponent: React.FC<TestComponentProps> = ({ title, }) => { return <div>TestComponent: {title}</div>; }; type TestComponent2Props = { bod ...

assigning token to angular headers

I've encountered an issue with my Angular 6 project where interceptors I added are not functioning properly. Despite injecting tokens into headers, the response I receive is an error with a status code of 401 (not authenticated). 401 (not authenticat ...

Comparison: executing an immediately invoked function expression (IIFE) and a separate

During my recent refactoring of some legacy code, I stumbled upon the following example: // within another function const isTriggerValid = await (async () => { try { const triggers = await db.any(getTriggerBeforeBook, { param ...

Creating a new array by combining data from two arrays: a step-by-step guide

Here is an array I have: response=[ { "mId": "4a993417-3dae-4a85-bb2e-c535d7fda6d7", "title": "test2", "score": "4", "id": "91ce873f- ...

Negative results encountered: Karma test on Chrome Headless failed due to an uncaught null exception

During the execution of unit tests, failures occur intermittently on different tests with no clear error message provided. ... Chrome 89.0.4389.114 (Linux x86_64): Executed 1225 of 1453 SUCCESS (0 secs / 3 mins 29.829 secs) Chrome 89.0.4389.114 (Linux x86_ ...