How do I retrieve a specific svg element in Angular among multiple elements?

I recently delved into learning Angular for a new project. One of my main objectives was finding a way to dynamically alter the styles of SVG elements. This led me to utilizing ViewChild and ElementRef.

Here is an example from the HTML:

<svg><g ...>
<polygon #someTag1 id = "s0" class="st27" points="67,46 58,46 58,56 67,56 67,70 86,70 86,32 67,32"/>
<polygon #someTag2 id = "s1" class="st27" points="104,46 113,46 113,56 104,56 104,70 85,70 85,32 104,32"/>
<polygon #someTag3 id = "s2" class="st27" points="147,46 138,46 138,56 147,56 147,70 166,70 166,32 147,32"/>

In the component file:

@ViewChild("someTag1") someTag1: ElementRef;

ngAfterViewInit() {
  this.someTag1.nativeElement.removeAttribute("class");
  this.someTag1.nativeElement.setAttribute("class", "shining");
}

This approach successfully achieved what I had in mind.

The challenge arises when I want to modify one element among a large group of diverse elements, not just #someTag1. With 260 different polygons, implementing this solution would require creating 260 ViewChilds...

I have been pondering using ViewChildren and developing separate components or classes for my SVG elements, but I am unsure how to proceed. Is there perhaps another alternative?

Your advice on this matter would be greatly appreciated.

Answer №1

It appears that the 200+ polygons are being generated through an iteration over a list.

some-other.component.ts

import { Component, AfterViewInit, ElementRef, QueryList } from '@angular/core';

@Component({
  selector: 'some-other-component',
  template: `<polygon #poly *ngFor="let item of items" />`
})
export class SomeOtherComponent
  items = Array(260)

  @ViewChildren('poly') refs: QueryList<ElementRef> ;

  ngAfterViewInit() {
    // perform actions on the polygons
  }

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

Retrieving the necessary data from my object to perform a sum calculation in angular

Having trouble retrieving an attribute from an array in my code. In my .ts file, I am fetching data from my backend endpoint like this: export class PostFeedComponent implements OnInit { data: any = {}; constructor(private http: HttpClient) { t ...

Optimal method for writing to JSON file in NodeJS 10 and Angular 7?

Not sure if this question fits here, but it's really bothering me. Currently using Node v10.16.0. Apologies! With Angular 7, fs no longer functions - what is the optimal method to write to a JSON file? Importing a JSON file is now simple, but how ca ...

Display permanent components in Angular while nesting components

Currently, I am incorporating the admin module within the main app module in my project Within the admin module, there are 2 components - register and login. Additionally, the admin module has its own routing module. Now, my task is to create a navbar sp ...

Issues with Angular routing in Fuse administrator and user interfaces

I am encountering an issue with navigating routes for admin and user roles, where the user role has limitations compared to the admin role. <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.1/angular.min.js"></script> const ...

Issue encountered: Jest database test does not end when using testcontainers

I am currently in the process of testing my database within my Node application written in Typescript. I have implemented Postgres 15 and Testcontainers for this purpose. Strangely, my code functions correctly when executed manually, with all clients being ...

Angular 2 Issue: @Input() Directive Not Recognized - Unresolved Reference Error

I am a beginner trying to grasp Angular2 concepts from ng-book 2(v49). Below is the code snippet from article.componenets.ts file: import { Component, OnInit } from '@angular/core'; import { Article } from './article.model'; @Componen ...

The sorting feature is not performing as anticipated

I'm dealing with an array of objects fetched from the backend. When mapping and sorting the data in ascending and descending order upon clicking a button, I encountered some issues with the onSort function. The problem lies in the handling of uppercas ...

Troubleshooting a Missing Call Display Issue in Angular2 API

Greetings, I am a new web developer and I have been tasked with creating a prototype Inventory Page using Angular2. Please bear with me as my code may not be perfect. In the snippet below, you'll notice that we are calling our base back-end API (&apo ...

Tips for validating and retrieving data from a radio button paired with an input box in reactjs

I'm diving into the world of React and facing a challenge with multiple radio buttons that have associated input fields, like in this image: https://i.stack.imgur.com/Upy3T.png Here's what I need: If a user checks a radio button with a ...

The error message "Angular 2 - Module not found: Error: Can't locate '@angular/material'" is indicating that the specified module or package is unable

I diligently followed every step outlined in the official guide: https://material.angular.io/guide/getting-started The Error I Encountered ERROR in ./src/app/app.module.ts Module not found: Error: Can't resolve '@angular/material' in ' ...

Securing redirection in WebPart using Azure AD: Best practices

I've successfully created a Sharepoint Webpart with a straightforward goal: authenticate users on an external website embedded within Sharepoint. This external site utilizes Azure AD for user logins. However, my current implementation has a significa ...

Angular 5 error: Decorators do not support function expressions

I am struggling to compile my angular project using the command ng build --prod The issue arises in the IndexComponent : index.componenent.ts import { Component, OnInit } from '@angular/core'; import { indexTransition } from './index.anim ...

Deleting an element from an array in Mongodb using Angular

My question remains unanswered: Delete an element from an array of an array in MongoDb In the process of creating a basic MEAN application similar to Stack Overflow, I have encountered an issue. Within my application, there are articles with upvotes and d ...

Angular array sanitization for handling multiple URLs

I need to sanitize multiple URLs from an array containing links to video sites e.g.: videos: SafeResourceUrl = ['www.someURL1', 'www.someURL2',... ]; To achieve this, I created a constructor like so: constructor(private sanitizer ...

What measures can I take to ensure that bcrypt does not negatively impact the speed of my website

Currently, I am undertaking a project that involves Angular and Node.js. The security of different passwords is ensured by using bcrypt. However, there is an ongoing issue that has been giving me some trouble. During the registration process for new users ...

The type {properties .....} is incompatible with the type ActionReducer<AdminState, Action> in Angular 12 using NGRX

Implementing NGRX library for redux to organize the state of the application in a structured way: export interface ApplicationState { adminState: AdminState } export interface AdminState { adminProductCategory: ProductCategoryState; adminProdu ...

Typescript: Potential null object error when defining a method

I recently encountered an error message stating "Object is possibly null" while working on the changePageSize method in book-store.component.html. It seems like I need to initialize the object within the class, but I'm not familiar with how to do that ...

Tips for removing the Y-Axis entirely from a CanavasJS chart

I'm working with a canvasJS chart and my goal is to completely hide the Y-Axis. I've managed to remove the Y-Axis line and title in this JSFiddle example. I came across these properties to hide the Y-Axis title and line, but I'm struggling t ...

:host-selector for Angular Material dialog

I am currently working with a dialog component provided by angular-material and I need to customize the appearance of the popup dialog. I am aware that there is some support for styling through the component generation: let dialogRef = dialog.open(MyDi ...

Mastering the Art of Mocking Asynchronous Methods in Node.js Using Jest

I have the following files: |_ utils.js |_methods.js I am currently conducting unit testing for rest.js methods, where the content of the file is as follows: methods.js import Express from 'express' import { add_rec } from './utils' ...