Ways to utilize the ::after pseudo class with ElementRef within Angular

Is it possible to manipulate the background-color of the ::after pseudo selector for the specified element using Angular?

@ViewChild('infobubble', { read: ElementRef }) infoBubbleElement: ElementRef;

  ngAfterViewInit(): void {
    const elem = this.infoBubbleElement.nativeElement;
    elem.style.backgroundColor = 'green';
  }

Answer №1

When faced with a similar situation, I encountered the limitation of not being able to access ::before or ::after HTML elements from elementRef. My workaround involved creating an additional DIV within the target HTML element that needed modification through a directive. This allowed me to change the background of the new div as if it were the ::before pseudo-element.

Here is an example of how I implemented my directive:

@Directive({selector: '[appPrimaryColor]'})

export class PrimaryColorDirective implements OnInit {

  @Input() backgroundColor = false;

  constructor(
    private elementRef: ElementRef,
    private _renderer: Renderer2,
  ) { }

  ngOnInit() {
    // Set BackgroundColor
    if (this.backgroundColor) {
      this._renderer.setStyle(this.elementRef.nativeElement, 'background-color', COLOR);
    }
  }
}

Implementation in the HTML:

<div class="icon">
  <!-- step-ok-effect is the new div-->
  <div
    appPrimaryColor
    [backgroundColor]="true"
    class="step-ok-effect"></div>
    <span class="icon-{{ step.state ? 'check' : step.icon }}"></span>
  </div>
</div>

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

Apply the "selected" class to the menu located in the common header

I have implemented a menu in the header.php file that will be displayed on all pages such as index.php or contact-us.php. However, I am facing an issue with adding the selected class to the active main category in the menu. The code provided works fine wh ...

Angular's detectChanges function does not trigger the ngDoCheck method

When I run cdr.detectChanges() in a nested child component (Child1) which has a parent and another nested child component (Child2), only ngDoCheck is invoked in Child2. Why doesn't it invoke DoCheck in the current component (Child1) as well? How can I ...

Adjust top position dynamically during resizing

When resizing the window, I am facing an issue with the image going outside of the box. I believe adjusting the position top of the image based on the box height might help in fitting the image properly within the box. $(window).resize(function() { va ...

Tips for aligning hyperlinks in the navigation bar using CSS3 and HTML5

Struggling to center the links in the navigation bar has been a challenge. I really want those buttons smack dab in the middle of the header bar, but despite trying everything, they stubbornly remain on the left-hand side. .header { overflow: ...

Leverage Angular2 components within Angular4 framework

Would it be possible to utilize angular2 components in angular4 projects? Specifically, I am interested in using a chart generation library from . However, it seems that the dependencies of this library are not compatible with my application. Are angular2 ...

Guide to applying Bootstrap styles to a specific section of an HTML document

What can I do to ensure that the Bootstrap library used by the main app does not affect the styling of my controls when integrating my web application inside the main app? I want to maintain the current styles of my controls without any interference from ...

Calibrating height using CSS

Within my HTML document, I have the following structure: <div id="content"> <div id="wrapper"> <div id="steps"> <form id="formElem" name="formElem" action="" method="post"> <fieldset class ...

Preventing data loss in an Ionic array - encountering issues with using this.array.push

When attempting to use the storage get method to fill the array storedArr = [], I encounter the error message .push is not a function: storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : []; The c ...

Creating a changing text color using Material UI and React

How can one dynamically set the text color based on the background color of a component using Material-UI API? If component A has a light background, then the text should be dark. For component B with a black background, the text should be light. (I have ...

Ensuring Angular Reactive Forms: Validation for Non-Empty Nested FormArray with Display of mat-error

I'm currently working on a project using Angular's reactive forms and facing a challenge with a nested structure. I need to validate that a FormArray of files within another FormArray is not empty. Each groupFiles can have multiple groups, and ea ...

I'm looking to create a Triangle shape with CSS, any tips on how to achieve

Similar Question: Understanding the mystery behind this CSS triangle shape Please provide me with your valuable suggestions. ...

The Vue 3 page does not allow scrolling unless the page is refreshed

I am encountering an issue with my vue3 web app. Whenever I attempt to navigate to a page using <router-link to ="/Dashboard"/> Here is the code for Dashboard.vue: <template> <div class="enquiry"> <div class= ...

The quantity of elements remains constant in the EventEmitter

The Grid component is structured as follows: export class GridComponent { @Output('modelChanged') modelChangedEmitter = new EventEmitter(); private valueChanged(newValue: any, item: Object, prop: string) { item[prop] = newValue; ...

Improving the performance of angular-map when adding numerous markers

I am currently working on a map project in Angular 7 that involves displaying 1600 markers, but I'm encountering slow loading times. Within my .ts file, I have a function that retrieves latitude and longitude values from a JSON file: private _popula ...

Exclude child nodes using a CSS or Xpath selector in Selenium WebDriver with Java

How can I use CSS or xpath selectors to retrieve the link text while excluding the text in the .muted class? Here is an example of the HTML structure: <a href="link"> Text <span class="muted"> –text</span> </a> Cur ...

Transforming HTML features into PHP scripts. (multiplying two selected values)

I am currently working on converting these JavaScript functions into PHP in order to display the correct results. I need guidance on how to use PHP to multiply the values of the NumA and NumB select options, and then show the discount in the discount input ...

Allowing Angular2 Components and their Sub Components to access a shared instance of ngModel within a service

Currently, I have been working on constructing a complex view that requires multiple functionalities. To ensure proper organization, I have divided it into various custom components. I don't want to go into great detail, but I have managed to make it ...

Different ways to restrict Table Header to 2 lines using CSS

When it comes to limiting text to a specific width, using white-space:nowrap is usually the go-to solution. However, I am facing a unique challenge where I need to make sure my header text fits within a specific width. I initially tried adding individual ...

Encountering an ElementNotInteractableException error message while attempting to select the YouTube search button using Selenium in conjunction with ChromeDriver

I recently updated my Python and Chrome.webdriver to the latest versions. I'm attempting to create a simple script for searching on YouTube, but encountering an error. Can anyone provide assistance? File "search.py", line 8, in <module> searchB ...

The specified starting point "@angular/material/card" cannot be accessed due to missing dependencies

When attempting to deploy a web application on Heroku, I encountered the following error message: - Generating browser application bundles (phase: setup)... Compiling @angular/core : es2015 as esm2015 Compiling @angular/common : es2015 as esm2015 Compiling ...