The Angular Material dialog fails to display content when triggered within an event listener in Google Maps

Within my project, I am utilizing Angular 6.0.6 and Angular Material 6.3.0. One issue I have encountered is with a dialog component that I have added to the entryComponents in the app module. Strangely, when I attempt to open this dialog within the rightclick event handler of a Google Maps marker, it appears empty most of the time.

Here is an example where the dialog shows up as empty:

private attachEvents(wo) {
  wo.marker.addListener('rightclick', (e) => {
    this.dialog.open(DialogAlert, {
      width: '400px',
      data: {
        confirmation: true,
        message: 'test',
        title: 'X'
      }
    });
  });
}

Interestingly, if I place the open code outside of wo.marker.addListener, the dialog functions correctly.

I would greatly appreciate any suggestions on how to make the dialog work properly inside the event handler. I've attempted calling detectChanges using ChangeDetectorRef within the event handler, but it does not resolve the issue.

The HTML code for the dialog is as follows:

<h1 mat-dialog-title>{{data.title ? data.title : defaultTitle}}</h1>
<div mat-dialog-content>
  <p>{{ data.message }}</p>
</div>
<div mat-dialog-actions>
  <button *ngIf="confirmation" mat-raised-button (click)="onNoClick()">Cancel</button>

  <button *ngIf="confirmation && !options" mat-raised-button (click)="onConfirmClick()" cdkFocusInitial>Yes</button>
  <button color="primary" *ngFor="let o of options; index as i" mat-raised-button (click)="onConfirmClick(i)">{{o}}</button>

  <button *ngIf="!confirmation" mat-raised-button (click)="onConfirmClick()" cdkFocusInitial>OK</button>
</div>

If I add content to the HTML before mat-dialog-title, mat-dialog-content, or mat-dialog-actions, it displays correctly.

Additionally, here are some observations:

  • No errors or warnings appear in the console.
  • I am unable to close the dialog by clicking on the shadow area (unless the content is displayed).
  • Occasionally, the content will display after a slight delay.
  • Resizing the browser window can trigger the content to appear in an empty dialog.
  • These tests were conducted using the latest version of Google Chrome.

Answer №1

Shoutout to @yurzui for the helpful comment - much appreciated! Here is the solution:

If you encounter any issues, try using ngZone with zone.run(

In this scenario, these are the changes that need to be made:

  1. Include private zone: NgZone in the component constructor.
  2. Invoke dialog using zone.run(.

The updated function now appears as follows:

private attachEvents(wo) {
  wo.marker.addListener('rightclick', (e) => {
    this.zone.run(() => {
      this.dialog.open(DialogAlert, {
        width: '400px',
        data: {
          confirmation: true,
          message: 'test',
          title: 'X'
        }
      });
    });
  });
}

PS. The actual code may vary slightly, but the essence and concept remain consistent. It's worth noting that I haven't tested the provided code snippet, so it might not work flawlessly if directly copied and pasted elsewhere.

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

"Disabling Dropzone.js functionality once a file has been selected - here's how

When using my dropzone, I typically choose a file to save first and then click the save button. However, I am facing an issue: how can I disable the dropzone area after selecting a file? I attempted the following approach: accept: function (file, done) { ...

The Angular 2 http request seems to be failing to reach the web api's get method when using a string parameter overload

Issue at hand is that the initial Get method gets triggered by this particular HTTP request: http://localhost:56690/api/testelements/?name=aeg One would anticipate the second (string overload) method to be invoked due to the presence of a string parameter ...

Error encountered when trying to match routes in two separate Angular applications within an Express app: "Cannot find any routes that match

Greetings (please pardon any language errors as English is not my first language) Like many others, I have an Angular app running in Express and decided to create separate Angular apps for the admin and users (mainly because the navigation bar was becomin ...

Concealing overflow in a sticky container when a fixed child element is present

I am currently working on a website where I have implemented slide-up section panels using position: sticky while scrolling. However, I am encountering issues with fixed elements within the sticky panels not properly hiding outside of the respective sectio ...

Can anyone explain to me how to render attributes of a tag in Vue? I'm curious about how v-for interacts with HTML

<ul> <span class="tabs" :class="{ activeTab: selectedTab === tab }" v-for="(tab, index) in tabs" @click="selectedTab = tab" :key="tab"> {{ in ...

Adjust the canvas size to fit its parent element ion-grid

I am currently working on an Ionic 3 component that displays a grid of images connected by a canvas overlay. I have utilized Ionic's ion-grid, but I am facing an issue with resizing the canvas. The problem is that I cannot determine the correct dimens ...

I'm encountering an unfamiliar error within my Discord.js bot, and I'm unsure of both its cause and the appropriate solution. Additionally, I'm unsure which specific file

I'm facing a recurring issue with my bot terminal. It's been causing me trouble for the past four days, functioning intermittently without any pattern. I'm struggling to track down the specific file where this error is originating from. Any ...

React Context Matters: Troubles Unleashed

I've been facing some difficulties with passing a value from one file to another. The problem seems to be related to implementing context, but I can't seem to figure out where I went wrong! import React from 'react' const Mycontext = ...

Convert the Date FR and Date US formats to ISO date format

There is a function in my code that accepts dates in different formats. It can handle two formats: 2022-06-04 or 04/06/2022 I want all dates to be in the format: 2022-06-04 For instance: public getMaxduration(data: object[]): number { data.forEach((l ...

What is the best way to isolate the elements from the specified dictionary that contain valid data?

I need to extract only the data for Flipkart from this array and create a new array containing just that information. json = [ { "amazon": [] }, { "flipkart": { "product_store": "Flipkart", ...

deleting the current product information during an ajax request

After executing the query provided below, I have $product_list in this code. However, I want to use ajax so that when I click on the button link1, $product_list gets emptied. How can I clear the content within products_list using an ajax call triggered by ...

Determine the width of two inline input fields

Showing two inputs side by side: +------------+ +--------------------------+ | ID='inputA'| | ID='inputB' | +------------+ +--------------------------+ +------------------------------------------+ A ...

Angular 2: Embracing the Power of Hierarchical Selection

My goal is to create cascading selects where each option in a dropdown menu can lead to its own set of unique child options. This will result in a hierarchical structure of selectable items. To accomplish this, I have defined a class named fieldSelect tha ...

How can we manually initiate a state change from the server side to the client view in Angular Universal?

We are currently working on an Angular Universal application built with angular/cli version 1.4.3 and node version 6.9.5. Our issue lies in the transition between server-side view and client view. The switch occurs before we have obtained all the necessar ...

Generating divs dynamically with inline styling and utilizing ngFor, while incorporating a conditional check with ngIf in Angular

Currently, I have an Angular component where I am dynamically generating div elements using the ngFor directive. However, I also need to validate a condition before creating each div, and I'm facing challenges when trying to use both ngFor and ngIf in ...

Exploring Symfony2: Enhancing user experience with dynamic form submission and dropdown menu updates

Starting from the beginning. I have a tab-panned layout with links. Upon clicking a link, a drop-down checkbox form is injected directly into the HTML through $(".dropdown-toggle").click(function() { var projectId = $("#permission-form").attr("data-p ...

Display exclusively the chosen option from the dropdown menu

I am facing an issue with the select input on my webpage. Whenever I try to print the page, it prints all the options of the select instead of just the one that is selected. Can someone please guide me on how to modify it so that only the selected option ...

What is the process for extracting the period value from SMA technical indicators within highcharts?

Can someone assist me in retrieving the period value from SMA indicator series by clicking on the series? series : [{ name: 'AAPL Stock Price', type : 'line', id: 'primary', ...

What is the process for running an older Angular project using ng serve?

I'm currently attempting to run a sample Angular project that is part of a third-party framework by using the command ng serve. It seems like this sample project requires Angular version ~4 based on the information in its package.json file. My global ...

Exploring the JSON data in Javascript using Ajax

Completely new to Javascript, I am just trying to grasp the basics of the language. Currently, I have a JSON request set up with the following code: function request(){ $.ajax({ dataType: "jsonp", type: 'GET', url: "getWebsite", ...