While performing compilation, Angular's ngFor triggers an error when used with SVG elements

I am attempting to create a recursive function of lines in order to generate a graph, but I am encountering a strange error in the console. It works fine on node.js. Below is the code snippet:

<svg height = "200" width = "300">
    <g *ngFor = "let node of nodes; let i = index; let last = last;">
      <line 
        [attr.x1] = nodes[i].x
        [attr.y1] = nodes[i].y
        [attr.x2] = nodes[i+1].x
        [attr.y2] = nodes[i+1].y
      />
      <line *ngIf = last
        [attr.x1] = nodes[i].x
        [attr.y1] = nodes[i].y
        [attr.x2] = nodes[i].x
        [attr.y2] = nodes[i].y
      />
    </g>
  </svg>

Below is the TypeScript code:

export class VisitsGraphComponent implements OnInit {
  nodes = [
    { x: 0, y: 0 },
    { x: 40, y: 120 },
    { x: 80, y: 80 },
    { x: 120, y: 90 },
    { x: 160, y: 40 }
  ]
  ngOnInit():void {

  }
}

I have attempted to simply use the node.x and place it in the ngOnInit(), but encountered the same error:

Cannot read property 'x' of undefined

Answer №1

You may encounter an error when attempting to access nodes[i+1] in the last item of your nodes array, as there is no element at that index. Consider using either a ngIf/else block or accessing nodes[i+1]?.x to avoid this issue.

Answer №2

There is an error in the data binding Correct nodes[i].x to > node[i].x Please make the necessary changes in the HTML code below:

.html

<svg height = "200" width = "300">
<g *ngFor = "let node of nodes; let i = index; let last = last;">
  <line 
    [attr.x1] = node[i].x
    [attr.y1] = node[i].y
    [attr.x2] = node[i+1].x
    [attr.y2] = node[i+1].y
  />
  <line *ngIf = last
    [attr.x1] = node[i].x
    [attr.y1] = node[i].y
    [attr.x2] = node[i].x
    [attr.y2] = node[i].y
  />
</g>

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

Getting started with Angular 2 and initializing component variables

Hey there, I'm new to angular2 and currently facing a challenge. Here's the Service section: getReports() { return this.http.get(GlobalVariable.BASE_API_URL + 'report/l', {headers: this.headers}).map(res => res.json()) ...

Issue with Angular currency code incompatibility on outdated Angular version

I am currently on Angular 7.3.x and unfortunately updating is not an option at the moment. I'm curious if this version limitation could be causing my issue. In essence: {{ value | currency:'USD' }} shows me $ {{ value | currency:'EUR ...

Error Handling in Angular Reactive Forms for Array Type Form Controls

I'm currently working on an Angular project that involves creating a reactive form with fields using FormArray. While I am able to detect and display the error status as "INVALID" for dynamic fields, I'm facing challenges in handling errors for c ...

Ways to invoke a function in Angular2 when the Boolean condition is met

Within my component class, I have implemented a popup function along with a Boolean flag that returns true or false based on specified conditions. In the template class, I want the popup function to be triggered when the flag becomes true, displaying a pop ...

The specified type `Observable<Pet>&Observable<HttpResponse<Pet>>&Observable<HttpEvent<Pet>>` is not compatible with `Observable<HttpResponse<Pet>>`

I'm currently attempting to integrate the Angular code generated by openapi-generator with the JHipster CRUD views. While working on customizing them for the Pet entity, I encountered the following error: "Argument of type 'Observable & ...

Setting form values using Angular 9

I am currently facing a challenge that I could use some assistance with. My dilemma involves integrating a new payment system, and I seem to be encountering some obstacles. Here is a snippet of what I have: options: PaystackOptions= { amount: 5000, emai ...

What is the process for defining child routes in Angular 2.0 for Dart?

When working with TypeScript, a child route can be easily defined as shown below: export const routes: Routes = [ { path: '', redirectTo: 'product-list', pathMatch: 'full' }, { path: 'product-list', component: P ...

Programmatically setting focus in Ionic

In my Ionic and Angular component, I am attempting to programmatically set focus after the page has loaded. Here is the code: item.component.html: <ion-row> <ion-col col-5> <ion-item > <ion-label&g ...

How can we pass an optional boolean prop in Vue 3?

Currently, I am in the process of developing an application using Vue 3 and TypeScript 4.4, bundled with Vite 2. Within my project, there exists a file named LoginPage.vue containing the following code: <script lang="ts" setup> const props ...

Tips for extracting an SVG path from HTML structure

This example I'm referencing is by Chris Coyer <svg version="1.1" xmlns="http://www.w3.org/2000/svg" mlns:xlink="http://www.w3.org/1999/xlink" style="display: none;"> <defs> <g id="icon-image"> <path class="path1" d="M0 4v26h32v ...

Unveiling the Mysteries of HTTP Headers in Angular

Seeking a way to retrieve a token set in the http header within my Angular application. This is how my Angular application is being served: var express = require('express'); var app = express(); var port = process.env.PORT || 3000; var router = ...

Deploying a nodejs application with angular as the user interface framework using Firebase

Is there a way to set up an express.js application with angular as the front-end framework, multiple route files, and communication between the server and angular via service level API calls, in order to deploy it on firebase using Firebase Hosting? Curre ...

What is the correct way to extract a value from a keyvalue pair?

When dealing with an object that returns boolean "issues", I specify it as a string. If the value is true, I aim to show a checkmark; if false, I want to display a cross. <ul *ngFor="let filtered of reposFiltered | keyvalue"> <li *ngIf=& ...

Adding a class dynamically to a <span> based on the value of a field or property

Learning Angular is still new for me, so please be patient. I have a TagComponent that includes a Color-enum and one of those colors as a property. I want Angular to automatically assign this color as a class so that Semantic UI can style it accordingly. ...

Testing the Snackbar function with Angular and TypeScript: Unit Testing

I've encountered an issue with a method called onDelete that utilizes a MatSnackBar which is injected in the constructor like so: constructor(private todoListService: TodolistService, private snackBar: MatSnackBar) { } onDelete(todoList: TodoList): v ...

Creating a Tree Hierarchy with Angular 4 FormArray

Looking for a demonstration on how to effectively utilize FormArray with a Tree Structure? I am currently working on implementing inline editing for a hierarchical system Although I have managed to make it functional for the initial level, I am facing ch ...

What is the best way to handle API requests within an Angular component?

I am currently diving into the world of Angular at my workplace, even though I do not have a background in web development. One challenge I am facing is how to encapsulate API calls within one of my components without knowing where to begin. The componen ...

Setting the default value in a Reactive form on the fly: A step-by-step guide

When creating a table using looping, I need to set the default value of my Reactive Form to `Repeat` if the loop value matches a particular character, otherwise I want it to be empty. Here is my code: typescript rDefault:string = ""; create(){ ...

Identify all the CHECKBOX elements that are visible and not concealed

On my page, I have various checkboxes - some with hidden=true and others with hidden=false attributes. Despite trying to use a selector or jQuery to locate checkboxes with the hidden property, I am still facing some challenges. My goal is to differentiate ...

showing javascript strings on separate lines

I need assistance with displaying an array value in a frontend Angular application. How can I insert spaces between strings and show them on two separate lines? x: any = [] x[{info: "test" + ',' + "tested"}] // Instead of showing test , teste ...