Apply and remove the CSS style during the initialization and destruction phases

After creating a component for file preview of audio and video, I decided to use the material dialog. However, I encountered an issue regarding the transparency of the dialog background.

In my SCSS component code, I attempted to set the background color of the dialog container to transparent using the following code:

::ng-deep .mat-dialog-container {
    background-color: transparent !important;
  }

Although this code worked for all dialogs, I needed it to apply specifically to the dialog within this component.

To achieve this specificity, I tried implementing a service called StyleService with the intention of applying the styles only to this component:

@Injectable({
    providedIn: 'root'
})

export class StyleService {
    // Service implementation details here
}

I then attempted to utilize the StyleService in the component as follows:


constructor(private styleService: StyleService) {}

ngOnInit(): void {
  this.styleService.addStyle('transparent-dialog-theme', require('../../them/dialogStyle.scss'));
}

ngOnDestroy(): void {
  this.styleService.removeStyle('transparent-dialog-theme');
}

Unfortunately, despite these efforts, the dialog background still did not appear transparent within the component. Can you help me understand what might be causing this issue and provide suggestions on how to resolve it?

Answer №1

If you need to customize the backdrop style of a dialog for a specific case, you can apply a class to the backdrop using the configuration settings.

For instance -

let dialogRef = this.dialog.open(ExampleDialogComponent, {
      width: '250px',
      data: { name: this.name, animal: this.animal },
      backdropClass: 'dialog-bg-trans'
    });

In your global styles, you can define the specific style like this -

.dialog-bg-trans {
  background-color: transparent;
}

This configuration should be applied only where needed, and not globally across all dialogs.

You can find an example here - https://stackblitz.com/edit/ab-angular-mat-dialog-bg-color?file=app%2Fexample%2Fexample.component.ts

I hope this information is useful.

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

Encountering a bug in Angular 10 while attempting to assign a value from

I am having trouble updating the role of a user. In my database, I have a user table and a role table with a ManyToMany relation. I can retrieve all the values and even the correct selected value, but I am encountering issues when trying to update it. Her ...

Pattern matching for a string with numerous repetitions using Regular Expressions

There's a [tree] and a cat and a [dog] and a [car] too. I am looking to find the words inside each set of square brackets. The resulting array will be tree, dog, car My attempt at using match(/\[(.*)\]/g) didn't work as expected. It ...

Error encountered in TypeScript Yarn application during download process: SyntaxError: Unforeseen symbol '.'

Today marks my first experience using yarn for applications, and unfortunately, I've encountered a frustrating issue: SyntaxError: Unexpected token '.' at wrapSafe (internal/modules/cjs/loader.js:915:16) at Module._compile (internal/ ...

Experiencing difficulties with AngularJs as onblur event is not functioning properly when the input field is empty

I am currently working on a form that allows users to input data. I am facing an issue where if a user enters a number, deletes it, and moves away from the input field, it will display an error message. However, I want the error message to display only i ...

Cross-Origin Resource Sharing (CORS) Ajax request using Jetty and Firefox

I'm attempting to perform a cross-domain POST request with jQuery using CORS. However, even after the OPTIONS request is successfully sent and responded to, the POST request does not go through as expected. My code for the jQuery post request is displ ...

Angular Material Themes - retrieve the current main color shade

My current setup includes two themes: Theme 1: Light $candy-app-primary: mat-palette($mat-blue, A700); $candy-app-accent: mat-palette($mat-orange, A200, A100, A400); $candy-app-warn: mat-palette($mat-red); $candy-app-theme: mat-light-theme($candy-app ...

How can I display a nested div when the parent div's v-if condition is not met?

In my project, I am utilizing Laravel 6 in combination with Vue.js 2. To iterate through users and showcase their information within div elements, I am using a for loop outlined below. The Category names are stored in user.pivot.demo2, and the users are ...

The function for converting a jQuery element to a DOM element is yielding a blank string as

What is the reason behind this code not working as expected: function getElementWidth(el){ return $(el)[0].style.width }; getElementWidth('#someElementIdId'); // returns -> "" However, when using this code... function getElementWidth(el){ ...

Where can the data be found within an AngularJS HTTP POST request?

Can someone help me with this issue? I'm trying to send data to my server using an AngularJS button that triggers a function with an HTTP post request. However, I can't seem to locate where the data is stored in the request. Here is the code for ...

Reloading content using AJAX

Index.php ... <form id="calculator_form" name="form" action="" method="get" > <input name="one" type="text"> <input name="two" type="text"> <input type="submit"> </form> ... <!-- reload area --> <?php if(isset( ...

Angular2 is throwing an error: "NavigationService provider not found! (MenuComponent -> NavigationService)"

I am in the process of developing an angular (beta7) application. I aim to have a MenuComponent at the top that utilizes the NavigationService to navigate throughout different sections of my app. To ensure that the NavigationService remains a singleton, I ...

Node.js: Determine if an object is missing a certain property

My goal is to check if a property 'lessons' (not inherited) is not present in the object. The following conditions all evaluate to true: (typeof obj['lessons'] == undefined) (!(obj.hasOwnProperty('lessons'))) (!(hasOwnPrope ...

Using Typescript and React to render `<span>Text</span>` will only display the text content and not the actual HTML element

My function is a simple one that splits a string and places it inside a styled span in the middle. Here's how it works: splitAndApplyStyledContent(content: string, textType: string, separator: string) { const splittedContent = content.split(separat ...

What is the reason behind child state resolve functions being executed prior to the parent state promises being resolved?

I am currently utilizing ui-router version 0.2.13. According to this page: All resolves on one state will be resolved before moving on to the next state, even if they aren't injected into that child Additionally, all resolves for all the states ...

Proper functionality of Chrome Extension chrome.downloads.download

I am currently developing an extension with a feature that allows users to download a file. This file is generated using data retrieved from the localStorage in Chrome. Within my panel.html file, the code looks like this: <!DOCTYPE html> <html&g ...

Identifying the Device Type within a Web-Based Program

In our Java application, we are looking to determine the type of device (mobile or desktop) that is making a request. Is there a way to achieve this? ...

Is there a way to remotely access and read text files stored on a server using either JavaScript or Ajax from an IIS6.0 Virtual directory?

Is it possible to use JavaScript or Ajax to read the text from a file on a remote machine (from IIS6.0 Virtual directory) and then copy it into a specific folder on the client machine? ...

Using TextField with nested Object for onChange Event

I have a question that needs some clarification. Here is the current structure of my object. Within "allgemein," there are currently two variables, but they will increase over time... { id: "Abadi", name: "Abadi", type: "SC", allgemein: { ...

Learn how to successfully import a webp image into a React TypeScript project

I have looked everywhere for the answer, but I can't seem to find it When trying to import a *.webp image in typescript, you need to create a declaration file, like declaration.d.ts The declaration file should contain something similar to the foll ...

Tips for customizing the AjaxComplete function for individual ajax calls

I need help figuring out how to display various loading symbols depending on the ajax call on my website. Currently, I only have a default loading symbol that appears in a fixed window at the center of the screen. The issue arises because I have multiple ...