0

I have a simple list that i would like to filter.

Example code:

<div *ngFor="let x of data"></div>

example data:

  data = [
        {
         "img" : "assets/img/photos/05.jpg",
         "title" : "Denim Jeans",
         "overview": "today"    
        },
        {
         "img" : "assets/img/photos/05.jpg",
         "title" : "Denim jacket",
         "overview": "month"

        },
];

I want to show only which have overview field month.

I expected something like this:

*ngFor = "let x of data == overview"

2 Answers 2

1

Try this:

<ng-container *ngFor="let x of data">
  <div *ngIf="x.overview === 'month'">
    <p>{{ x.title }}</p>
  </div>
</ng-container>

The ng-container is just a logical construct that allows you to add conditions/loops/etc without them having a structural element on the page.

So, in your case the container loops though everything in the array and the inner div determines if the div appears due to the check on the x.overview value.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use custom pipe to filter data like below

<div *ngFor="let x of (data | overviewMonth)">
   {{x}}
</div>



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



@Pipe({ name: 'overviewMonth' })
export class overviewMonthsPipe implements PipeTransform {

  transform(alloverViews: data[]) {
  return alloverViews.filter(x => x.overview === 'month');
 }
}

Refer https://angular.io/guide/pipes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.