Guide on manipulating the CSS class of an external library component within our custom component in Angular

My component includes an external library component, and I need to toggle one of the classes in the external component based on a condition in my own component. Since I cannot use ngClass for this purpose, I am hesitant to utilize document.querySelector. Are there any alternative methods available?

Answer №1

If you wish to reference an external library component in your Angular component class, you can utilize the ViewChild decorator. By setting the read option of ViewChild to obtain the component as an ElementRef, you will gain access to toggle the class of the DOM element.

For instance, if the external component within your component's template is structured as follows:

<div>
  <external-component class="toggle-me"></external-component>
</div>

You can assign a template reference variable to it like this:

<div>
  <external-component #exComp class="toggle-me"></external-component>
  <!--                ^^ add this template reference variable  -->
</div>

Next, within your component class, employ ViewChild to obtain the external component using the specified template reference variable while specifying { read: ElementRef } to retrieve its actual DOM element instead of its class instance:

@ViewChild('exComp', { read: ElementRef }) externalComponent: ElementRef;

Subsequently, you can then manipulate the nativeElement and its

classList</code property to toggle the designated class:</p>
<pre class="language-js"><code>this.externalComponent.nativeElement.classList.toggle('toggle-me');

In another scenario where adding a template reference variable is not feasible, you have the alternative of passing the class name of the external component to ViewChild rather than the variable name:

@ViewChild(ExternalComponent, { read: ElementRef }) externalComponent: ElementRef;

To observe these strategies in action, refer to this StackBlitz example.

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

Display additional javascript code for expanding a marquee

Currently, I am working on a stock ticker project using JavaScript. It's progressing well, and now I am focusing on adding a "show more" button to style the ticker. The button should be placed outside of the marquee. When clicked, it will expand the m ...

Showing a variety of pictures within a specified time frame

In my CSS, I have defined classes that allow me to display different background images on a page at set intervals: .image-fader { width: 300px; height: 300px; } .image-fader img { position: absolute; top: 0px; left: 0px; animation-name: imagefade; ...

Ways to trigger the display of an image upon hovering over a button without using inheritance

<div class="a"> <img class="img_c" id="img_id" src="~~"> <div class="b"> <div class="c"> <ul class="d" id="e"> <li ~~~ > </ul> </div> </div> </div> Is there a way to display the ...

Angular progress bar with dynamic behavior during asynchronous start and stop

Currently, I am facing an issue with the progress bar functionality while utilizing the ng-bootstrap module. The scenario involves a dropdown menu with multiple options, and my desired behavior includes: The ability to start/stop the progress bar indepen ...

Customize your file input with Bootstrap 4 using bs-custom-file-input and create a Symfony 5 collection with dynamic upload fields

The issue of not having a label for the chosen filename in a bootstrap 4 upload field has been resolved using the plugin https://github.com/Johann-S/bs-custom-file-input You can utilize "bs-custom-file-input" to showcase the selected filename in the boots ...

What is the functionality of observable in Angular? The 'includes' property is not present in the 'Observable' type

I am currently diving into the world of Angular5 and I have been using Firebase to fetch data for my page display. While researching how to retrieve data from Firebase using AngularFire, I found that many examples were outdated. Eventually, I learned that ...

unusual occurrences related to live reloading in Angular 6

I am currently working with Angular 6 and I am facing an issue with live reload functionality. The problem I am encountering is that live reload works fine when I update app files. For instance, when I update app.module.ts, the live reload feature works p ...

What is the reason for deprecating the practice of utilizing data and errors in the subscribe/observable method in Angular?

What is the reason for this code being deprecated? And what is the proper format? fetchPeople() { this.peopleService.fetchPeopleList().subscribe( (data) => { console.log(data); }, (error) => { console.log(error); } ); } ...

The loading feature of jQuery UI Autocomplete - .ui-autocomplete-loading is ingenious

When fetching the XML file for the search box, I would like to apply this css. It currently takes around 3 seconds to load the file. In the autocomplete.js file, I found these two functions: _search: function( value ) { this.term = this.element ...

I Tried Adding Up All the Numbers, but It Doesn't Seem to Work for Every Dynamic Total Field

In my project, I am utilizing Laravel 5.7 and VueJs 2.5.*. The issue I am facing is that when I input values, the Total field calculates correctly. However, when I dynamically add rows for items, the calculation only works for the first row and not for the ...

Angular Material Toolbar Experiencing Color Distortion

To see the issue in action, check out this CodePen: An ongoing problem I've encountered with Angular Material involves distorted colors on the toolbar. The edges of the toolbar display one shade of green, while the middle shows a different shade. Tak ...

Executing a function upon the loading of a template in AngularJS

I have been searching through various responses here, but I haven't come across one that addresses my specific problem. Apologies if this has already been answered. I am a beginner in angular js and have recently begun working on angular routing. I am ...

What is the best way to ensure that my cards maintain consistent proportions?

My cards are not maintaining their proportions, causing buttons to pop out of the card and the card dimensions changing. I realize this issue is due to using fixed width and height in combination with flex. I'm struggling to find the best solution to ...

Eliminate screen flickering during initial page load

I've been developing a static website using NuxtJS where users can choose between dark mode and default CSS media query selectors. Here is the code snippet for achieving this: <template> <div class="container"> <vertical-nav /> ...

Utilizing Regular Expressions to remove specific CSS attributes from an HTML content

I'm facing a challenge with my telerik RadEditor where users can input HTML that gets saved to my database. While this usually works well, I encounter issues when the CSS attributes position: absolute; or z-index: 100; (any # for z-index) are present ...

Leveraging a ScrollView in an AbsoluteLayout with Angular Nativescript

I have developed a unique photo browsing app that allows users to swipe between pages to view different photos. Currently, I have implemented a working view for this feature. <ScrollView orientation="horizontal" ios.pagingEnabled="true" > <Stac ...

I want to transfer a static .html file from a Spring Boot application to an Angular application and display it within a specific component

Recently diving into the world of Angular and facing a challenge. I have a task where a .html report is created post executing selenium test cases in my backend, which happens to be a spring-boot application. My next step involves sending this .html repor ...

Issue with web bot functionality

My dynamic database page is filled with an array of hyperlinks. The layout consists of 3 columns: The first column pulls links directly from the database, the second adds a + link to each entry, and the third includes a - link Essentially, every row that ...

`The value of an element within an array changes automatically`

In my current setup, I have a traditional array where each element represents an HTML element. The issue arises when I manipulate these elements within the array; any changes made to the HTML element also reflect in the array automatically. However, I pref ...

Using Bootstrap to handle opening a link when a dropdown menu button is clicked

Utilizing Bootstrap in my main menu was essential for navigating through the numerous pages, subpages, and sub-subpages within my project. The dropdownmenu feature proved to be very helpful in this regard. However, my client now has a specific request - t ...