Different Ways to Modify Data with the Change Event in Angular 8

How can I dynamically change data using the (change) event? I'm attempting to alter the gallery items based on a matching value. By default, I want to display all gallery items.

public items = [{
    value: 'All',
    name: 'All Items'
  },
  {
    value: 'Photos',
    name: 'Photo Items'
  },
  {
    value: 'Video',
    name: 'Video Items'
  },
];
<div *ngFor="let item of items" class="nav-li">
  <ul>
    <li class="value" [value]="item.value"><a href="">{{item.value}}</a></li>
  </ul>

</div>

<div class="gallery">
  <div *ngFor="let item of items" class="card">
    <div class="data">{{item.name}}</div>
  </div>
</div>

If you click on "Photos," it should show only photo items instead of displaying all items by default.

Answer №1

Check out this HTML demo with a custom pipe

<div *ngFor="let item of items | customFilter : filter" class="card">
    <div class="data">{{item.name}}</div>
</div>

Set the initial value of the filter in the component to 'All'

filter='All'

Add a click method for each list item

<ul>
   <li class="value"(click)="updateFilter(item.value)" [value]="item.value"><a href="">{{item.value}}</a></li>
</ul>

In the component file, define the onChange function

updateFilter(value){
  this.filter=value;
}

This example demonstrates how to use a custom pipe in Angular

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'customFilter'
})

export class CustomFilterPipe implements PipeTransform {
  transform(data: any[], filterValue:string): any {
      return data.filter(x => filterValue == 'All' || x.value == filterValue);       
  }
}

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

Unable to establish a connection between Laravel websocket and Angular

The CORS policy has blocked access to XMLHttpRequest from the origin 'http://localhost:4200' to the resource at 'http://localhost:8080/socket.io/?EIO=3&transport=polling&t=MgBuvgw' because there is no 'Access-Control-Allow- ...

Trouble extracting and utilizing GraphQL queries in Typescript with relay-compiler

I attempted to utilize relay with the Typescript react starter, but I am encountering several problems. It appears that babel-plugin-relay is unable to detect the graphql statements extracted by the relay-compiler. Below is my compiler script: "relay": " ...

Unable to display label in form for Angular 2/4 FormControl within a FormGroup

I'm having trouble understanding how to: Use console.log to display a specific value Show a value in a label on an HTML page Display a value in an input text field Below is my TypeScript component with a new FormGroup and FormControls. this.tracke ...

"Ionic Calendar 2 - The ultimate tool for organizing your

I am using a calendar in my Ionic app that retrieves events from a database through an API. var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://portalemme2.com.br/SaoJoseAPI/agenda', true); this.http.get('http://portalemme2.c ...

Troubleshooting: Why is my ng serve command in Angular 5 not

Whenever I try to run ng serve, an error pops up ng : The term 'ng' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is ...

Struggling to position a div below another div within a flex box layout

https://i.sstatic.net/8vZSW.jpg When trying to make the Information & advice header appear above the other div elements, I encountered an issue with flexbox. Despite setting the container div to have a flex direction of column, the header div continue ...

To modify the pageSize of the KendoGrid in order to display 1-10 out of 100, you can customize the appearance and functionality of the page numbers by changing

I am currently working on a kendo grid that utilizes the traditional [kendoGridBinding]="gridData" method as shown below - `<kendo-grid #grid [kendoGridBinding]="gridData" [pageable]="true" [pageSize]="5" style="cursor:pointer"> <kendo-grid ...

Is it possible to use non-numeric values as keys in a Typescript Map?

During a lesson: private items: Map<number, string> = new Map(); ... this.items[aNumber] = "hello"; Results in this error message: An element has an any type because the expression of type number cannot be used to index type Map<numbe ...

Angularfire2 combined with Bootstrap Modal will enhance the user experience of

Hello everyone, I am currently facing an issue while trying to integrate Bootstrap modal with AngularFire2 in Angular. The problem lies in the fact that the data retrieved from the database is not being recognized. <button *ngFor="let utilfaq of (uti ...

Change object values to capital letters

Upon retrieving myObject with JSON.stringify, I am now looking to convert all the values (excluding keys) to uppercase. In TypeScript, what would be the best way to accomplish this? myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", ...

In TypeScript, the first element of an array can be inferred based on the second element

Let's consider a scenario where we have a variable arr, which can be of type [number, 'number'] or [null, 'null']. Can we determine the type of arr[0] based on the value of arr[1]? The challenge here is that traditional function ov ...

angular2 the custom template validator is encountering outdated values

I've been struggling with a specific issue for quite some time now. I'm working on setting up an Angular 2 custom validator that checks if a number falls within a certain range. When used as follows, everything functions correctly: <input typ ...

My type is slipping away with Typescript and text conversion to lowercase

Here is a simplified version of the issue I'm facing: const demo = { aaa: 'aaa', bbb: 'bbb', } const input = 'AAA' console.log(demo[input.toLowerCase()]) Playground While plain JS works fine by converting &apo ...

conflict between ng-content and textarea elements

My custom component called my-textarea has a unique template structure: <textarea> <ng-content></ng-content> </textarea> However, when I try to input text into the component using the same method as a regular textarea HTML eleme ...

When creating a new instance of a class in Angular, the methods for initialization are not included

I have a User class set up like this: export class User{ id: number = 0; fName: string = ""; lName: string = ""; constructor(){} fullName() { return this.fName + ' ' + this.lName; } email ...

Issue with installing angular CLI on Windows 10

Having trouble installing Angular CLI with npm on a Windows 10 system I've exhausted all possible solutions Attempted clearing the cache and re-running the installation command - npm install -g @angular/cli - but encountered new errors every tim ...

Update the content of a page on an Ionic application every few minutes

I am currently developing an application that requires the data displayed on the page to be refreshed automatically every 15 minutes, even if there is no user interaction happening at that time. What would be the most efficient method to accomplish this t ...

Searching with Mat-autocomplete across multiple fields simultaneously

I am struggling with a mat-autocomplete that contains 5000 objects, each consisting of a first name and a last name. My goal is to search for both the first name and last name simultaneously regardless of the input order. Currently, I can only search one ...

Update an API call to switch from promises to observables, implementing axios

Recently, I have been experimenting with using rxjs for API requests in a React application and this is the approach that I have come up with. What are your thoughts on this method? Are there any best practices that you would recommend following? If you ...

Angular searchbar issue: Unable to assign parameter of type 'unknown' to parameter of type 'string'

I've been working on an Angular app to fetch data from an API successfully. However, I'm currently facing a challenge while implementing a search function in my component.ts file. Here is the entire service code: import { Injectable } from &apos ...