Execute specific CSS styles based on certain conditions for elements within an *ngFor iteration

I have a dynamically populated list of objects.

My goal is to change the background color of the div that contains a checked checkbox.

Additionally, I would like to display buttons when the checkbox is selected.

<div *ngFor="let item of items" [ngClass]="classname: ifCheckboxChecked">
  <p-checkbox (onChange)="onCheckboxChecked($event)"></p-checkbox>
  <span>{{item.itemName}}</span>
  <div *ngIf="ifCheckboxChecked">
    <button></button>
    <button></button>
  </div>
</div>

Answer №1

To optimize your code, consider introducing the “isChecked” property into your item’s model. This implementation would enhance the clarity and efficiency of your program. Here is a potential representation:

<div *ngFor="let obj of objects" [class.customClass]="obj.isChecked">
  <p-checkbox [(value)]="obj.isChecked"></p-checkbox>
  <span>{{obj.objectName}}</span>
  <div *ngIf="obj.isChecked">
    <button></button>
    <button></button>
  </div>
</div>

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

Exploring the process of retrieving two distinct values from a local JSON file

In my home page, I want to display categories and in the details page, I want to show menuItems. Currently, I am able to retrieve and parse categories in home.ts. How can I parse both categories and menu items in my typescript file, and fetch only menu ite ...

Function modifies global variable

What could be causing the global variable to change when using the function write_ACK_ONLY()? I'm passing the array rxUartBuffer to write_ACK_ONLY() as data = new Array(20), but upon checking the Log Output, it seems that the function is also modifyin ...

Executing HTTP requests in Angular 2 using Ajax calls

Currently, I am in the process of fetching user data from a database to display on my Angular 2 page. The setup involves using Angular 2 as a separate application and utilizing ReST APIs to retrieve the data. On the backend, I have a spring boot microser ...

Best practices for implementing dual ngFor directives within a single tr element?

Click here to view the page The image attached shows the view I want to iterate through two ngFor loops inside the tr tag. When using a div tag inside the tr, it's looping the button next to the tag instead of where I want it in the file table header ...

the "then" function is causing issues in my TypeScript Node code

My code looks like this: var fs = require('fs'); var util = require('util'); var files = fs.readdirSync('*/path to my folder that contains subfolders*/') async function getfilenum(){ var files_v_num = [] for(const i in fi ...

Step-by-step guide on creating a graph similar to the one shown in the image

I am currently new to Angular and I am encountering difficulties with creating graphs. I am unsure how to implement this in Angular. I have tried using both Chart.js and Canvasjs, but I am having issues with both: 1. With Chart.js, I was able to create a ...

Error: The program encountered a type error while trying to access the '0' property of an undefined or null reference

I am a beginner in the world of coding and I am currently working on creating an application that allows users to add items to their order. My goal is to have the quantity of an item increase when it is selected multiple times, rather than listing the same ...

Comparing compiling a TypeScript application and running it with Node.js versus running it with ts-node

Which option is more resource-efficient: compiling my TypeScript application and executing it with Node.js, or using ts-node to run it directly without compilation beforehand? I am working on a TypeScript application that utilizes connections to both MySQ ...

Facing issues with Angular 6: encountering errors related to the module "rxjs/add/operator/map" and receiving another error indicating that 'map' does not exist on type 'Observable<Response>'

Currently in my Angular 6 project, I encountered two errors: ERROR in ./src/app/app/img/img.service.ts Module not found: Error: Unable to locate 'rxjs/add/operator/map' in '/Users/user/Projects/A4/imageSearch/src/app/app/img' ERRO ...

What is the procedure for incorporating a cookie jar into axios using typescript?

I encountered an issue while trying to add a cookie jar to an axios instance. The problem arises because the interface AxiosRequestConfig does not have a member named "jar". Is there any way to enhance the existing AxiosRequestConfig type or is there a wor ...

Implement a nested filter in Angular 8 to enhance data manipulation

Is it possible to apply a filter inside another filter in this scenario? I have a table of orders with nested tables of services, and I want to only display the orders where the type of service is 'type1'. I tried using the following line of code ...

Error Handler: Unable to retrieve the error object when utilized with a promise

I'm currently working on an angular application with a custom error handler implementation. One interesting point to note is that when you have a custom error handler and use an observable with http without catching errors using a catch block, like i ...

How do React Native proxies differ from vanilla React proxies in their functionality?

Trying to retrieve data from an API running locally on port 5000 of my device, I recalled setting the proxy in package.json when working on React web applications: "proxy": "https://localhost:5000" Attempting to fetch information f ...

ForkJoin Observable failing to trigger events

Having an issue with implementing forkJoin on two Observables in my code. Despite directly subscribing to them yielding a response, the forkJoin function doesn't seem to be triggering. Any suggestions on what might be causing this? private data$: Obse ...

When attempting to run npm install -g @angular/cli, there are no valid versions found for the undefined package

Currently operating on Windows 10 Enterprise. I am attempting to install Angular CLI for running an Angular project. I have input the following command. --> npm install -g @angular/cli Unfortunately, I encountered the following error. --> npm E ...

Develop a React npm package with essential dependencies included

I have been working on developing a UI library using React ts. As part of this project, I integrated an external library called draft-js into the package. However, when I attempt to implement my library in another project, I keep encountering errors. Despi ...

Automate the vertical resizing of a div element using code

Currently, I am utilizing Angular4+. I am looking to dynamically resize a div vertically, but I am unsure of how to go about it. I am uncertain about where to even begin and how to accomplish this task without resorting to using jQuery. Are there any sugg ...

How can a div be displayed next to another div when it is clicked using angular2?

Is there a way to have another page show up after clicking on a div, like shown in the image below? Before Click: https://i.sstatic.net/qSBbc.png After Click: https://i.sstatic.net/julH4.png ...

There seems to be an issue with the functionality of Angular's updateValueAndValidity() method

I am facing an issue with a simple form setup: <form [formGroup]="form" (ngSubmit)="onSubmit()" novalidate> <input type="radio" id="enabled" formControlName="enabled" [value]="true" ...

Encountered Error: Unknown property 'createStringLiteral', causing TypeError in the Broken Storyshots. This issue is happening while using version ^8.3.0 of jest-preset-angular

When I upgraded to jest-angular-preset ^8.3.0 and tried to execute structural testing with npm run, I encountered the following error: ● Test suite failed to run TypeError: Cannot read property 'createStringLiteral' of undefined at Obje ...