Is Angular CLI incorrectly flagging circular dependencies for nested Material Dialogs?

My Angular 8 project incorporates a service class that manages the creation of dialog components using Angular Material. These dialogs are based on different component types, and the service class is designed to handle their rendering. Below is a simplified version of this service:

@Injectable()
export class MyService {
    renderDialogTypeOne() {
        // method implementation that uses matDialog.open(TypeOne)
    }

    renderDialogTypeTwo() {
        // method implementation that uses matDialog.open(TypeTwo)
    }
}

The challenge arises from the fact that the service class references the types it renders, resulting in a dependency. However, one of the rendered types (TypeTwo) contains an instance of the aforementioned service class within its constructor. This is necessary for TypeTwo to launch its own dialogs of TypeOne:

export class TypeOne {
}

export class TypeTwo {
    contructor(private service: MyService) { }

    showNestedDialog() {
        this.service.renderDialogTypeOne();
    }
}

This situation can be perceived as a circular dependency between the service class and TypeTwo. While splitting the service class into multiple parts could potentially resolve this issue, it might not be ideal to do so solely to address a compiler warning.

Does this truly constitute a circular dependency? If so, isn't this a common occurrence where two entities have a sort of chicken-and-egg relationship?

Are there any feasible solutions other than simply turning off Angular's circular dependency notifications?

Answer №1

The code snippet from the Angular Material repository related to the Dialog component reveals the use of an Injector for creating the Component that appears within the Dialog window. This method helps in avoiding circular dependencies as discussed on Stack Overflow.

Consequently, it seems that the warnings about potential circular dependencies are not accurate.

If needed, one can opt to disable these circular dependency warnings globally by tweaking the angular.json configuration file. Unfortunately, this adjustment cannot be applied at a per-file level.

angular.json

....
  "defaults": {
    ....
    "build": {
      "showCircularDependencies": false
    }
  }

Workaround

To address scenarios where nested calls might occur, consider using the following approach. For instance, a Dialog belonging to type DialogYesNoComponent could launch another Dialog of type DialogWarningComponent, and vice versa.

Example

import { DialogService, DialogYesNoComponent, DialogWarningComponent } from '...'


export class TypeOne {
  constructor(private dialog_service: DialogService) { }

  // Method to prompt a yes/no dialog
  showYesNoDialog() {
    const dialog_question = "Would you like to continue?";
    const dialog_ref: MatDialogRef<DialogYesNoComponent> =
      this.dialog_service.open_yes_no_dialog({
        question: dialog_question,
        title: 'Confirm', height: '300px' })
    dialog_ref.afterClosed().subscribe(
      (choice: 'yes' | 'no') => {
        if (choice === 'yes') {
          // Continue with program logic
        } else {
          // Open Nested Warning Dialog
          this.showWarningDialog("Stopping the program.");
        }
      }
    )
  }

  // Display warning dialog
  showWarningDialog(warning: String) {
    ...
  }
}

DialogService

import { ElementRef, Injectable } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material';

import { DialogWarningComponent } from './dialog-warning/dialog-warning.component';
import { DialogYesNoComponent } from './dialog-yes-no/dialog-yes-no.component';

@Injectable()
export class DialogService {
  constructor(public dialog: MatDialog) { }

  // Method to open a yes/no type dialog
  public open_yes_no_dialog({ question, title = 'Confirm', yes_button_first = true,
    has_backdrop = false, height = '250px', width = '350px' }:
    {
      question: string, title?: string, yes_button_first?: boolean, has_backdrop?: boolean,
      height?: string, width?: string
    }): MatDialogRef<DialogYesNoComponent> {

    const dialog_ref = this.dialog.open(DialogYesNoComponent, {
      autoFocus: true,
      backdropClass: 'cdk-overlay-transparent-backdrop',
      closeOnNavigation: true,
      disableClose: false,
      hasBackdrop: has_backdrop,
      height: height,
      width: width,
      data: { question: question, title: title, yes_button_first: yes_button_first }
    })

    return dialog_ref
  }

  // Method to display warning dialog
  public open_warning_dialog() {
    { warning, title = 'Warning',
    has_backdrop = false, height = '250px', width = '350px' }:
    {
      warning: string, title?: string, has_backdrop?: boolean,
      height?: string, width?: string
    }): MatDialogRef<DialogWarningComponent> {

    const dialog_ref = this.dialog.open(DialogWarningComponent, {
      autoFocus: true,
      backdropClass: 'cdk-overlay-transparent-backdrop',
      closeOnNavigation: true,
      disableClose: false,
      hasBackdrop: has_backdrop,
      height: height,
      width: width,
      data: { warning: warning, title: title }
    })

    return dialog_ref
  }
}

DialogYesNoComponent

import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';


export interface YesNoDialogOptions {
  question: string
  title: string
  yes_button_first: boolean
}


@Component({
  selector: 'dialog-yes-no',
  templateUrl: './dialog-yes-no.component.html',
  styleUrls: ['./dialog-yes-no.component.css']
})
export class DialogYesNoComponent {
  constructor(public dialog_ref: MatDialogRef<DialogYesNoComponent>,
    @Inject(MAT_DIALOG_DATA) public options: YesNoDialogOptions) { }
}

DialogWarningComponent

import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';


export interface WarningDialogOptions {
  warning: string
  title: string
}


@Component({
  selector: 'dialog-warning',
  templateUrl: './dialog-warning.component.html',
  styleUrls: ['./dialog-warning.component.css']
})
export class DialogWarningComponent {
  constructor(public dialog_ref: MatDialogRef<DialogWarningComponent>,
    @Inject(MAT_DIALOG_DATA) public options: WarningDialogOptions) { }
}

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

Substituting generic type in inherited class method results in error message: "Property 'x' in type 'y' cannot be assigned to the property with the same name in the base class 'Parent'."

Here is the code snippet I am working with: class BaseModel { protected getAttribute<T extends BaseModel>(): keyof T | null { return null; } } class Payment extends BaseModel {} class Item extends BaseModel {} class Sale extends BaseModel { ...

Disable a tab or menu item if the bean's boolean value is 'false'

I designed a home page with various menu/tab options that redirect the user when clicked, but only if they have access to that specific item. If access is not granted, the menu item becomes unclickable. While this functionality works, I am interested in en ...

Error TS2339: The code is trying to access a property 'f' that is not defined within the 'ReactiveComponent' type

I have recently started working with Angular and I encountered an issue while creating a reactive form. The form is supposed to display the submitted values in an array, but when I added validators to the password field, an error popped up. error TS2339: ...

A script object must only permit one property at a time

I am unfamiliar with TypeScript and I have an object named obj with 3 properties: a, b, c. However, it is important to note that b and c cannot exist together in the same object. So, my object will either be: obj = { a: 'xxx', b: 'x ...

Video showcasing issue with updating Ionic2 view

I am encountering an issue where my View is not updating as expected. When a player makes a move, the triggers work (I did a console log on every update of the current game [1]), but the View does not update regularly. To investigate this problem, I added ...

Encountering a 404 Error while attempting to access a locale in Angular 5

Currently, I am facing an issue while trying to load a locale in my Angular 5 project that is being run from within VS 2015. According to the documentation, to add a locale, I should include the following code in the app.module: import { NgModule, LOCALE_ ...

Having trouble calling a method from one component to another using its instance

Trying to call a method from one component to another is causing some issues. I have created an instance of the component and tried calling its method, but it doesn't seem to be working as expected. In the code snippet below, you can see that I am try ...

Determine whether an array is void, then proceed to deactivate a button

I am attempting to prevent a button from being clickable if an array is empty, but I am encountering difficulties. <button [disabled]="(users.length ==0 )?true:false">Send mass emails</button> Within the TypeScript file: users: UsersModel[]; ...

Tips for checking dropzone functionality during development

I'm currently working on an Angular 5 application and incorporating dropzonejs (the angular wrapper) into it. However, as I am not the backend developer, I do not have full visibility into how the backend has been developed. Currently, CORS is being ...

`How can I enhance the appearance of an Angular 4 component using an attribute?`

There is a component where I need to pass specific data to the child components within an ngFor loop using attributes. Depending on the attribute, I want to style these child components accordingly. Code testimonials.component.html - (Parent component) ...

Routing in nested modules for Angular 7

I am struggling to understand how the routing works with multiple nested modules. The RouteTree seems correct to me, but I am facing an issue after navigating to the "ContactPage" where the URL changes but the view does not render. Below is a snippet of m ...

Production environment causing issues with router.url functionality

Encountering an issue in production (server with Apache) with the Router component of Angular version 4.4.6. Below is the code snippet: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Params, Router} from '@angul ...

Adding a new document to an existing collection with an array field in MongoDB

Having an issue with adding a new chapter to my var array. Here is the code snippet in question: array.push({ chapter: [ { id: 2, title: 'adsf', content: &ap ...

Creating a function in Angular to locate each object based on its ID

Hello there, I am currently working on creating a method called findChildByIdInData(data:any, childId:string). This method will take in a JSON main node with children that have unique IDs, and the goal is to find a specific object based on the ID provided ...

TSLint flagging a parsing issue in type guard while TypeScript compiler fails to pick up on any errors

I am facing an issue with my TypeScript method that includes a type guard: export function isArray(item: any): item is Array<any> { return item.constructor === Array; } After running tslint on the file, I encountered the following error message ...

Using Vivus.js in an Angular 5 Component

I am currently facing some challenges while attempting to incorporate Vivus.js library into Angular 5. The issue seems to be arising from the constructor of Vivus, which suggests that the library is being loaded correctly but not recognizing my element id. ...

Unable to loop through the data using forEach method as it is not a function. Can anyone guide me on how to

Currently, I am working on an angular project that involves retrieving data from a JSON object. Typically, in such cases, I would create a class like 'fake.ts' to fetch and access the correct values. However, in this instance, the JSON contains 4 ...

Guide to implementing the patchValues() method in conjunction with the <mat-form-field> within the (keyup.enter) event binding

I am currently working on a feature that populates the city based on a zip code input. I have successfully achieved this functionality using normal HTML tags with the (keyup) event binding. However, when trying to implement it using CSS, I had to use (keyu ...

Can you guide me on how to specify the return type in NestJS for the Session User in a request?

async authenticated(@Req() request: Request) { const user = request.user return user } It is important for the 'user' variable to have the correct type globally. While working with express passport, I came across the following: decl ...

I am having trouble getting the guide for setting up a NextJS app with Typescript to function properly

For some time now, I have been experimenting with integrating Typescript into my NextJS projects. Initially, I believed that getting started with Typescript would be the most challenging part, but it turns out that installing it is proving to be even more ...