Clicking on the element will toggle the class, adding it if it's

When I click on an item, I want to add a class name. If I click on the same item again, I want to remove the class for ngFor loop.

<ion-item *ngFor="let x of statementresponse; let i=index" class="cust_delay delay" [ngClass]="{'active': selectedItem == x}" (click)="listClick($event, x)" >
</ion-item>

selectedItem: any;
listClick(event, newValue) {
    console.log(newValue);
    this.selectedItem = !newValue;
}

Answer №1

To achieve this functionality, consider incorporating an "active" property into your item objects:

items = [
  {name:'one', active:false},
  {name:'two', active:false},
  {name:'three', active:false},
];

In your template, display the items like so:

<li *ngFor="let item of items" 
    (click)="toggleClass(item)" 
    [ngClass]="{'active': item.active}">{{ item.name }}</li>

The toggleClass() function can be used to toggle the active state of the clicked item:

toggleClass(item){
  item.active = !item.active;
}

Example

Answer №2

Give this a go:

HTML Snippet

<ion-item *ngFor="let x of statementresponse;let i=index" 
     class="cust_delay delay"[class.active]="selectedItem == i" 
     (click)="selectedItem=i" >
      {{x}}
</ion-item>

Typescript Code:

selectItem=-1

Check out the StackBlitz demo here

Answer №3

To keep track of the value, you can save a boolean in the object itself. Then, use the code snippet below:

<ion-item [class.active]="item.active"></ion-item>
item.active represents the boolean value

When clicked, toggle the boolean value like this:

item["active"] = !item["active"]

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

No declaration file was located for the module '@handsontable/react' despite the presence of a 'd.ts' file

Embarking on a fresh project using vite+react+ts+swc by executing the command below as per the vite documentation. npm create vite@latest -- --template react-swc-ts Additionally, I integrated the handsontable library into my setup with the following comm ...

Expanding MaterialUi styled components by incorporating TableCellProps: A guide

When trying to create a styled TableCell component in a separate file, I encountered an error in TypeScript (ts(2322)). TypeScript was indicating that the properties "component," "scope," and "colSpan" could not be used because they do not exist in StyledC ...

Maintain division contents while navigating

I need a solution to keep my div (NewCall) open even when the user navigates between pages in my Single Page Application. The NewCall div is located in my layout page and is triggered by user click. However, whenever I navigate to another page, the div cl ...

What is the best way to transform a JavaScript object into a chain of interconnected links?

My goal is to transform an object structure like the one below... var obj1 = { firstName: 'John', lastName: 'Green', car: { make: 'Honda', model: 'Civic', revisions: [ { miles: 10150, code: & ...

Creating a function that allows for the dynamic addition of rows in Angular with

I am currently working on implementing search and filter functionality, which requires adding rows dynamically. With hundreds of fields to filter, my boss has decided to organize them in dropdown menus (such as Manager, City, and Name in this example). The ...

What is preventing the union distribution from occurring with T[number] when T is an ArrayLike?

Below, in this demonstration (linked playground, I anticipate that Foo<[0, 1]> and Bar<[0, 1]> will both be resolved to 0[] | 1[] due to the distribution of unions in conditional types. However, in actuality, Foo<[0, 1]> ends up being (0 ...

Exploring ways to establish communication between parent and child components through click events

I am currently working on an Angular 8 application that involves a parent-child relationship. The parent component, DossierCorrespondenceComponent, contains a function that needs to be triggered from within the child component. This is the function in th ...

Using TypeScript with Redux for Form Validation in FieldArray

My first time implementing a FieldArray from redux-form has been quite a learning experience. The UI functions properly, but there seems to be some performance issues that I need to investigate further. Basically, the concept is to click an ADD button to i ...

CSS: Ensuring consistent spacing between a custom popover and its control, regardless of container size modifications

I'm experimenting with creating a dynamic popover that adjusts its position based on whether it's viewed on a mobile device or desktop. Although I've made progress, the challenge lies in ensuring reusability. When resizing the container, th ...

Using Typescript to customize component props and theming through CSS Modules

Currently, I am in the process of building a react component library using typescript and CSS Modules to simplify the project. However, I am facing challenges with typescript interfaces, especially when it comes to theming variants for my components. For ...

When you utilize useSelector, the state may be returned as undefined even after it has been initialized

In the process of developing a project that mirrors Twitter(X), my approach involves implementing state management with REDUX and maintaining persistent login using redux-persist. I am storing the user's username and token in both the Redux store (for ...

subscribing to multiple observables, such as an observable being nested within another observable related to HTTP requests

Hello, I recently started learning Angular and I am facing a challenge with posting and getting data at the same time. I am currently using the map function and subscribing to the observable while also having an outer observable subscribed in my component. ...

No errors encountered during compilation for undefined interface object types

Hey there, I'm currently exploring the Vue composition API along with Typescript. I'm facing an issue where I am not receiving any errors when an interface does not align with the specified types for that interface. Although my IDE provides aut ...

Ways to filter and display multiple table data retrieved from an API based on checkbox selection in Angular 2 or JavaScript

Here is a demonstration of Redbus, where bus data appears after clicking various checkboxes. I am looking to implement a similar filter in Angular 2. In my scenario, the data is fetched from an API and stored in multiple table formats. I require the abili ...

Utilizing Carousel Functionality in Angular 4

I've added a Carousel to one of my components. Bootstrap has been correctly imported (verified by testing a bootstrap button). The active image, slider icons, and navigation icons at the bottom are all visible. However, the images are not sliding and ...

Tips for validating a receipt in an In-App purchase within an Ionic1 or Ionic2 application on iOS

Currently, I am working on integrating InApp purchases into an iOS app built with Ionic. However, I am facing difficulties in validating the receipt. Despite successfully obtaining the transaction ID and receipt post-purchase. ...

Angular CLI yields a unique arrangement of files

As a newcomer to angular2, I have been exploring various tutorials to enhance my knowledge. The tutorials I've been following have a specific file structure, as shown below: Link to tutorial corresponding to the image below: https://i.sstatic.net/7s ...

CRITICAL ALERT: Mark-compact operations inefficiently close to heap threshold resulting in allocation failure - JavaScript heap exhausts memory capacity #34576

Upon executing ng build --prod, I encounter the following issue: 10% building 3/7 modules 4 active ...le-line-icons\css\simple-line-icons.cssBrowserslist: caniuse-lite is outdated. Please run next command npm update 90% chunk assets ...

How can I resolve the problem of transferring retrieved data to a POST form?

When it comes to the form, its purpose is to send data fetched from another API along with an additional note. The fetched data was successfully received, as I confirmed by logging it to the console. It seems that the form is able to send both the fetche ...

What could be causing the ERROR TypeError in an Angular form where "_co.service.formData" is undefined?

My attempt to create a form in Angular 7 has resulted in an error message: ERROR TypeError: "_co.service.formData is undefined" Here is the HTML code for the component: <form (sumbit)="signUp(form)" autocomplete="off" #form="ngForm"> <div clas ...