Show a table row based on a specific condition in Angular

I'm having this issue with the tr tag in my code snippet below:

<tr *ngFor="let data of list| paginate:{itemsPerPage: 10, currentPage:p} ; let i=index " *ngIf="data.status=='1'" >
<td>

</td>
<td>
</td>
</tr>

I am trying to display the tr only when

*ngIf="data.status=='1'"
is true.

However, I encountered the following error:

Can't have multiple template bindings on one element. Use only one attribute prefixed with *

If anyone has a solution for this problem, I would greatly appreciate it. Thank you!

Answer №1

Approach 1: Enclose the content within <ng-container>.

<ng-container *ngFor="let data of list | paginate:{itemsPerPage: 10, currentPage:p} ; let i = index">
  <tr *ngIf="data.status == '1'">
    ...
  </tr>
</ng-container>

Approach 2: Implement the filtering in the component

this.list = this.list.filter((x) => x.status == '1');
<tr *ngFor="let data of list | paginate:{itemsPerPage: 10, currentPage:p} ; let i = index">
  ...
</tr>

Demo Approach 1 & 2 @ StackBlitz

Answer №2

To achieve this, you have the option of utilizing ng-template: https://angular.io/api/core/ng-template

One approach is to encapsulate the entire content within <ng-template> and then apply the structural directive like so:

<ng-template [ngIf]="data.status=='1'">
    <tr
        *ngFor="let data of list| paginate:{itemsPerPage: 10, currentPage:p} ; let i=index "
    >
        <td></td>
        <td></td>
    </tr>
</ng-template>

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

Dividing the text by its position value and adding it to a fresh object

I needed to divide the paragraph into sections based on its entityRanges. Here is what the original paragraph looks like: { type: 'paragraph', depth: 1, text: 'Do you have questions or comments and do you wish to contact ABC? P ...

Error: There was a problem trying to import the `.d.ts` file

Software Stack Vue version 3.2.19 @vue/test-utils version 2.0.0-rc.15 Typescript version 4.1.6 Issue Description Encountering an error when running the command vue-cli-service test:unit --no-cache. Please refer to the TypeError screenshot (link to Con ...

Accessing property values from a map in Angular

Is there a way to retrieve a property from a map and display it in a table using Angular? I keep getting [object Object] when I try to display it. Even using property.first doesn't show anything. //model export interface UserModel { room: Map ...

The Influence of Getter Performance in Angular Templates

As I delve into an existing Angular application, I've noticed a pattern where values used in templates across many components are actually properties that are being accessed through getters and setters without any additional logic: <input type="nu ...

Ways to break down a collection of multiple arrays

Looking to transform an array that consists of multiple arrays into a format suitable for an external API. For example: [ [44.5,43.2,45.1] , [42, 41.2, 48.1] ] transforming into [ [44.5,42], [43.2,41.2] , [45.1, 48.1] ] My current code attempts this ...

Creating a personalized installation pathway for NPM, Angular, and TypeScript on Windows operating systems

Is there a way to prevent NPM and Angular from using the folder C:\Users\XXXXXX\AppData\Roaming\npm in Windows? I am trying to globally install Node.Js and Angular on a different disk (using the command NPM i --prefix D:\MyF ...

Prevent repetition of errors during compiling in the ahead-of-time (AOT

I need assistance with optimizing my codebase in Angular 2 using angular-cli. When I run the command "ng build --prod", I encounter an error that is preventing the output of the dist folder. This error claims that there is a duplicate identifier in one of ...

Arranging mat-checkboxes in a vertical stack inside a mat-grid-tile component of a mat-grid-list

I'm looking to create a grid list with 2 columns, each containing 2 checkboxes stacked vertically. While I found this helpful question, it still involves a lot of nested divs. Is there a cleaner way to achieve this layout? Here's how I currentl ...

What is the process for switching the tile layer from OpenStreetMap to Stamen?

Exploring the possibilities of using Stamen maps with ngx-leaflet has piqued my interest. For those interested, more information on integrating leaftlet can be found here. However, the process of integrating it with ngx-leaflet remains a bit unclear to m ...

What is the best way to dynamically set an ID in Angular using the pound symbol (#)?

I am currently developing a dynamic Angular 6 application that utilizes dynamic components. My approach involves using @ViewChild('<id>', { read: ViewContainerRef }) <id>; to reference the divs where I intend to populate with dynamic ...

"Exploring the process of connecting a `mat-select` element with dynamically changing data

Having trouble binding data from an API to a mat-select control due to the asynchronous nature of the call. The issue arises because the mat-select control is set before the results are fetched. Any suggestions on how to resolve this? app.component.html ...

Angular does not completely erase everything

Having some issues with file deletion while working on angular and typescript. My setup involves three interfaces: Project, SubProject, and Position. When a subproject is added to the selected project, it gets included in the subProjectIds list of the Proj ...

Tips for incorporating nested generics in Typescript

Currently, I am developing a straightforward activity execution framework that allows developers to define activities which can be executed within a workflow. To enhance type safety and boost developer productivity by utilizing type hints, I aim to incorp ...

Encountering an error on Ionic 4, Angular 8: Unable to access property 'subscribe' of undefined

I have a project that utilizes Ionic 4 and Angular 8. The project requires integration with a payment gateway service, which I am accomplishing using the Ionic 4 InAppBrowser plugin to open the payment gateway site. This is how I've implemented it: ...

Tips for implementing simple custom styling within an Angular application without relying on innerHTML

Seeking advice on the best practices for a specific scenario. I am currently developing a small Angular application where users can input text. I would like to allow them to easily make certain words bold or create links. For example, if they type *whatev ...

What could be causing the issue of my application not being able to operate on the specified port on Heroku?

After spending two whole days trying to decipher the Heroku error message with no success, I'm still unable to pinpoint what is causing the issue. 2021-07-18T04:27:08.741998+00:00 app[web.1]: {"level":30,"time":1626582428741,&quo ...

Achieving the highest ranking for Kendo chart series item labels

Currently, I am working with a Kendo column chart that has multiple series per category. My goal is to position Kendo chart series item labels on top regardless of their value. By default, these labels are placed at the end of each chart item, appearing o ...

Utilize Angular6 and NodeJS to exhibit images stored on a server file system

Successfully uploaded images to a server but struggling to find a tutorial on how to display these images using Angular6 and NodeJS. Any help would be greatly appreciated. Thank you in advance. Edit: After many trials and errors, I was able to retrieve a ...

I am struggling to comprehend the concept of dependency injection. Is there anyone available to provide a clear explanation for me?

I am working on a NestJS application and trying to integrate a task scheduler. One of the tasks involves updating data in the database using a UserService as shown below: import { Injectable, Inject, UnprocessableEntityException, HttpStatus, } fro ...

Webpack is struggling to locate core-js paths when running on Windows operating systems

When running webpack, I am encountering the following errors: ERROR in ./node_modules/core-js/index.js Module not found: Error: Can't resolve './es' in 'pathtoproject\node_modules\core-js' @ ./node_modules/core-js/index. ...