Parsing errors occurred when using the ngFor template: Parser identified an unexpected token at a specific column

In my Angular CLI-built application, I have a component with a model named globalModel. This model is populated with user inputs from the previous page and displayed to the user in an HTML table on the current page. Here's how it's done:

<input type="radio" [(ngModel)]="criteria1" name="parameter1Identifier">Class A
<input type="radio" [(ngModel)]="criteria1" name="parameter1Identifier">Class B

<input type="radio" [(ngModel)]="criteria2" name="parameter2Identifier">Type 1
<input type="radio" [(ngModel)]="criteria2" name="parameter2Identifier">Type 2

<table>
<thead>
  <tr *ngIf="tableColumnCount">
    <th *ngFor="let colNumber of tableColumnCount | rangePipe">
      <input type="checkbox"
             [checked]=false
             (click)="selectAllElementsInColumnOfTheTableIn(colNumber);">
      select column {{ n + 1 }}
    </th>
  </tr>
</thead>

<tbody>
  <tr *ngFor="let row of globalModel | modelToArrayOfArraysFilter: criteria1: criteria2;">  
     <td *ngFor="let acol of row; let i = index;">    
         ....
     </td>
  </tr>
</tbody>
</table>

The total number of columns in the table is determined by the variable totalColumnsOfTheTable, which is set as follows in the component:

set totalColumnsOfTheTable(count: number) {
    if ( this.tableColumnCount < count ) {
      this.tableColumnCount = count;
      this._cd.detectChanges();
    }
}

The rows in the table can contain varying numbers of elements like [obj], [obj1, obj2], [objA, objB, objC], [objN], etc. The data displayed is filtered based on user-selected criteria1 and criteria2 radio buttons. The aim is for the table to reflect this filtering. When trying to use *ngFor with

totalColumnsOfTheTable = row.length
, Angular throws an error.

I attempted using trackBy but found that referring to ‘component’ as

this.totalColumnsOfTheTable = index
does not work. I'm seeking advice on determining the total column count or alternative methods to set component variables within the *ngFor loop. Any assistance or suggestions on the approach would be greatly appreciated.

Answer №1

Don't overlook adding the let before declaring your new variable. It should appear as follows:

<td *ngFor="let acol of row; let i = index; let totalColumnsOfTheTable = row.length">...</td>

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

Error message displayed: "There was an issue locating (undefined) and (undefined) while attempting to run Angular-CLI command 'ng g c my-new-component'."

Issue: I'm experiencing a problem in my Angular 5+ app project. When I try to generate a new component in one of my subfolders using the command ng g c my-component, nothing is being generated. Instead, I am encountering some unknown errors. I have a ...

The function causes changes to an object parameter once it has been executed

I've encountered an issue with a function that is supposed to generate a string value from an object argument. When I call this function and then try to use the argument in another function, it seems to be getting changed somehow. Here is the code fo ...

Is there a way to use Css Autoprefixer on multiple files at once with a single function

My project built with Angular2 contains over 100 CSS files that do not have any prefixes for browsers like Mozilla or Chrome. I am looking for a tool, gulp task, or anything else that can automatically add prefixes to all of my CSS files. I attempted to u ...

Storing service data within Angular2 components

As I work on creating a login service called AuthService for my Angular 2 application, I'm facing unexpected challenges. My main goal is to have a single user object that remains consistent throughout the entire application after a user logs in. This ...

Using Angular 2+, learn how to effectively utilize and monitor the output emitter when utilizing the createComponent factory method

When I dynamically create a component in ParentComp, the code looks like this: const factory = this.resolver.resolveComponentFactory<ChildComp>(component); this.componentRef = this.container.createComponent(factory); this.componentRef.instance.dataC ...

Utilizing Async/Await with Node.js for Seamless MySQL Integration

I have encountered two main issues. Implementing Async/Await in database queries Handling environment variables in pool configurations I am currently using TypeScript with Node.js and Express, and I have installed promise-mysql. However, I am open to usi ...

Tracking user session duration on a React Native app can be achieved by implementing a feature that monitors and

I'm currently focusing on tracking the amount of time a user spends on the app in minutes and hours, and displaying this information. I have successfully implemented the functionality to count minutes, but I am struggling to figure out how to track wh ...

Exploring the sharing of observables/subjects in Angular 2

UPDATE: After further investigation, it appears that the SharedService is being initialized twice. It seems like I may be working with separate instances, causing the .subscribe() method to only apply to the initiator. I'm not sure how to resolve this ...

Exploring Angular 4's capability: Incorporating data from Http Post Response into a .js file or highchart

I'm a beginner with Angular 4. I'm trying to create a dashboard that pulls data from an Http Post Response and I want to use this data to create a Chart (Highchart). I have successfully received the response in the console.log and formatted it i ...

Is the use of a Backend-for-Frontend necessary when working with a framework such as Angular?

For my frontend application, I'm utilizing Angular and connecting to an existing backend service for data retrieval. This backend service is established as a legacy system that I don't have control over. To enhance security, I've integrated ...

Troubleshooting an Angular application in Intellij using Chrome on a Windows operating system

I've been searching for a long time for a way to debug an Angular app in IntelliJ using Chrome on Windows. So far, I have not been successful in attaching a debugger to Chrome. I have tried launching Chrome with --remote-debugging-port=9222 and numer ...

"Launching" conduit for Observable

Is there a way to attach two pipes to an HttpClient request in order to execute functions at the beginning and end of the request? I have discovered the "finalize" operator for executing a function when the request is finished, but I am looking for an equi ...

Vue3 and Ionic combined to create a Component that became a reactive object in Vue

Vue is issuing a warning about receiving a Component as a reactive object, which can cause unnecessary performance overhead. The warning suggests using markRaw or shallowRef instead of ref to avoid this issue. However, in my code, I am not explicitly using ...

Tips for ensuring the HTML checkbox element is fully loaded before programmatically selecting it

When a user accesses the page, I want certain checkboxes to be automatically checked. To achieve this, I am storing the IDs of the HTML checkbox elements in a service. Upon entering the page, the service is utilized to retrieve an array containing these ID ...

Tips for defining the type of a parameter in a Vue Component

Is it possible to define a type 'VueComponent' for a component parameter in TypeScript? function callback(component: VueComponent???){ // some code } ...

Using React MUI to implement a custom Theme attribute within a component

I have a CircularProgress element that I want to center, and to make the styling reusable, I decided to create a theme.d.ts file: import { Theme, ThemeOptions } from '@mui/material/styles' declare module '@mui/material/styles' { inte ...

Obtaining the accurate return type based on the generic parameter in a generic function

Is there a way to determine the correct return type of a function that depends on a generic argument? function f1<T>(o: T) { return { a: o } } // How can we set T to number through (n: number)? type T1 = typeof f1 extends (n: number) => infe ...

Error: Unable to iterate over data.data due to its type

I am attempting to fetch images from the woocommerce API and here is the code I am using: this.config.getWithUrl(this.config.url + '/api/appsettings/get_all_banners/?insecure=cool') .then((data: any) => { this.banners = data.data; consol ...

Error encountered while reading JSON data using Angular4 and TypeScript: Json

When a user selects one or more checkboxes and submits a form, data in the database is updated. At that moment, I call location.reload() from the code to reload the page and display the correct data. Below is the backend web API code: [HttpGet] public as ...

How can I turn off the draggable feature for a specific element in react-beautiful-dnd?

Currently, I am implementing a drag and drop functionality using TypeScript with react-beautiful-dnd. The goal is to allow users to move items between two containers - one containing user names and the other labeled "Unassigned". Here is a snapshot of the ...