Using an asynchronous pipe filter with the ngFor loop in Angular 2 for efficient data

I have a JSON array that I need to iterate through in order to display data using an NGfor Loop. My goal is to apply filters after the data has been loaded to refine the results. The issue I am facing is that my pipe filter is returning 'cannot read property of undefined'. What mistake am I making? To simplify, I have omitted the http Request to fetch data as well as the return logic on the pipe statement.

// component 
import { Observable } from 'rxjs/Rx';
currentData:any = httpRequest; // Simplified  

// html 
<div *ngFor="let item of currentData | async | datePipe; let i = index">
 //display content
</div>

// pipe
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'datePipe'
})
export class DatePipe implements PipeTransform {

  transform(time:any []): any[] {
    console.log(time);
    return time.filter(it => it["createDt"] != -1); // Simplified

  }

}

*Updated fix *

   // html 
    <div *ngIf="currentData?.length > 0">
      <div *ngFor="let item of currentData | datePipe; let i = index">
        //display content
      </div>
    </div> 

   //pipe 
    transform(time:any): any {
     for(let key of time){
        console.log(key.somevalue);
     }
  }

Answer №1

   // HTML code 
    <div *ngIf="currentData?.length > 0">
      <div *ngFor="let item of currentData | datePipe; let i = index">
        // Display the content here
      </div>
    </div> 

   // Custom pipe example 
    transform(time:any): any {
     for(let key of time){
        console.log(key.somevalue);
     }
  }

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

Incorporate the npm mqtt package into my Angular application

I'm trying to incorporate the package https://www.npmjs.com/package/mqtt#connect into my Angular project. Since it's not a standard Angular package, I'm unsure of what I need to include in the module and controller to make it function correc ...

Microsoft Edge version 41.x is causing issues with cutting off AngularJs 1.x expression markup

Encountering a problem when using basic angular syntax in the Microsoft Edge browser. It appears that Edge cuts off lengthy angular expression markup unexpectedly. As an example: ng-disabled="conditionOne || conditionTwo || conditionThree" Gets shorten ...

resetting dropdown selections upon page refresh using jQuery and AJAX

Is there a way to reset or clear the values of two select boxes after refreshing the page in CodeIgniter? Currently, both select boxes retain their values after a refresh. Below is the code I am using: <?php echo form_dropdown('cat_id', $ ...

What is the best way to reference an i18n entry within .json files?

I am in the process of creating a user interface using SAP UI5 and my goal is to make all text elements internationalized. Below is a snippet of my view in .xml format where I successfully retrieve text in different languages using placeholders enclosed i ...

Can an entire object be bound to a model in an Angular controller function?

In TypeScript, I have defined the following Interface: interface Person { Id: number; FirstName: string; LastName: string; Age: number; } Within a .html partial file, there is an Angular directive ng-submit="submit()" on a form element. A ...

How can JSON Jackson utilize a read-only property alias for maintaining backwards compatibility?

We recently discovered a typo in our configuration structure, where the word periodicity was mistakenly spelled as period*o*city. The example source below has been corrected, but we still need to ensure that old configuration files can be read for backward ...

Having trouble retrieving the variable from a newly created filter in an ng-repeat loop in AngularJS

Here is an example of my ng-repeat: <a class="item item-avatar" ng-click="loadProfile({{student.pi_id}})" ng-repeat="student in filteredStudents = (students | filter:model.txtSearch)" > <img src="./img/man.png"> <h2 ...

Having trouble with updating React state? The useEffect hook runs when attempting to change the state, but it seems to have the

Having worked with useEffect and its ability to trigger after a state variable has been updated, I am well-versed in its functionality. I'm currently drafting this post on my phone while away from home. Here's the setup I have: const [dateValue ...

Preserve the array's status following a personalized filter in AngularJS

I am currently working with two arrays of data displayed in a tree structure and have established a relationship between them. $scope.types=['odd','prime','square','even']; $scope.items=['1','4', ...

What is the best way to retrieve the identity or specifics of the item that was right-clicked within the context menu

My AngularJS populated table includes a link button. When I right-click on this button, I've created a specific context menu using jQuery that pops up. The issue arises when I try to retrieve the ID of the item I clicked on in the context menu (such a ...

Creating a flexible parent tag for grouping child elements using Angular 2

My Objective I am determined to develop an Angular 2 button component that can dynamically render as either an <a /> tag or an <input /> tag based on the value of a property called type. Furthermore, this button component should be able to acc ...

Polling with RxJs, handling errors, and resetting retryCount with retryWhen

I am currently working on setting up a polling strategy that functions as follows: Retrieve data from the server every X seconds If the request fails, show an error message: "Error receiving data, retry attempt N°"... And retry every X seconds for a maxi ...

Duplicate occurrences of cookie values are causing issues

I am currently working on a search bar feature that saves users' search queries in a cookie. The issue I'm facing is that duplicate values are being saved in the array when the user types the same query again. I need to find a way to prevent dupl ...

Guide to accessing a menu through Long press or Right click in Angular2

I recently started experimenting with angular 2 and I am trying to figure out how to create a menu that opens with multiple options on both mobile and desktop devices. What I'm looking for is a way to trigger the opening of a menu when a long hold or ...

Hiding the right CSS border for every even number in AngularJS using a loop

I am currently in the process of building an application using AngularJS and the Ionic framework. My goal is to display a right border only for odd numbers. Below is the snippet of my code: <div class="media-body" style="padding-bottom:25px;"> ...

When trying to access a property in Typescript that may not exist on the object

Imagine having some data in JS like this example const obj = { // 'c' property should never be present a: 1, b: 2, } const keys = ['a', 'b', 'c'] // always contains 'a', 'b', or 'c' ...

Using JSON parsing to extract an integer as the key in JavaScript

I've been searching for almost 2 hours now, but I can't seem to find anyone using an integer as the key in their json object. The structure of my json object is as follows: {"342227492064425": {"added":"2020-10-04T23: ...

What is the process for selectively adding interceptors to app.module?

After searching through various topics, I have not found a solution that addresses my specific issue. To provide some context, we have an Angular App that operates in two modes - one mode uses one API while the other mode utilizes a different API. My goal ...

Improving the method of retrieving RTK result within getServerSideProps

Currently, I am utilizing RTK Query to make an API call within my getServerSideProps function. While I can successfully retrieve the result using the code snippet provided below, I find the process somewhat awkward. Additionally, the result lacks proper ty ...

Is it true that System.Text.Json.Deserialize is still unable to handle TimeSpan in .NET Core 5?

Recently, I encountered an issue with a TimeSpan JSON string that was generated using JsonSerializer.Serialize<TimeSpan>(MyTymeSpan): The jsonString looks like this: {"Ticks":1770400500000,"Days":2,"Hours":1,"Mill ...