Tips for activating list-groups upon clicking in Angular 5

I am currently experimenting with Bootstrap list-groups and Angular 5. I have encountered an issue where clicking on a specific item in the list-group activates all items instead of just the one clicked. Is there a way to enable only the clicked item in the list-group?

-- Bootstrap 4
<ul class="list-group">
  <li class="list-group-item">Cras justo odio</li>
  <li class="list-group-item">Dapibus ac facilisis in</li>
  <li class="list-group-item">Morbi leo risus</li>
  <li class="list-group-item">Porta ac consectetur ac</li>
  <li class="list-group-item">Vestibulum at eros</li>
</ul>

-- my list
<ul class="list-group">
  <li class="list-group-item" [class.active]="!isActive" (click)="onClick(s)" *ngFor="let s of $speech | async" [value]="s.$key">
    {{s.speech}}
    <i class="fa fa-chevron-right"></i>
  </li>
</ul>

-- from the components

 isActive = false;

  onClick(s) {

    console.log(s)

    this.isActive = !this.isActive;
  }

Answer №1

To keep track of the currently active item, a straightforward solution would be to use the item's index.

Here is an example of how you can achieve this using the following HTML code:

<ul class="list-group">
  <li class="list-group-item" [class.active]="active === i" (click)="onClick(i)" *ngFor="let s of ($speech | async); let i = index;" [value]="s.$key">
    {{s.speech}}
    <i class="fa fa-chevron-right"></i>
  </li>
</ul>

Don't forget to update your component as well:

active: number;
onClick(index: number) {
  this.active = index;
}

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

Having trouble with my Angular subscription - not behaving as I anticipated

I am facing an issue on my shop page where I have a FilterBarComponent as a child component. On initialization, I want it to emit all the categories so that all products are rendered by default. However, on my HomePageComponent, there is a button that allo ...

"Customizing text input colors in Angular Material: A step-by-step guide

Welcome to my User Form: https://i.stack.imgur.com/7eLp9.jpg I'm facing an issue where the color of the input field is white, blending with the background color. I've been searching for how to change the color of the input field without any luc ...

Do Observables serve as an extension of Promises?

Understanding the distinction between these two concepts is not the purpose of this particular question. I have encountered this inquiry twice already and I have yet to come across any online resources that adequately clarify it. Does the creation of obs ...

What method would you recommend for modifying HTML text that has already been loaded using JSP?

Is there a way to update text on an HTML document without reloading the entire page? I'm looking to create a simple "cart" functionality with 5 links on a page. When a link is clicked, I want it to increment the "items in cart" counter displayed on th ...

How to make Angular resolver and component share an injected service?

In my products list component, I have a table displaying various products. Since there is a considerable amount of data, I implemented a resolver to prevent the user from being directed to the page until all the data is loaded. The resolver currently utili ...

How Jquery Can Be Used to Dynamically Add Extra Rows in HTML Without Submitting Data via POST Requests

When using jQuery to dynamically add extra rows to an HTML table, I encountered an issue where the fields of the extra rows were missing in the $_POST array when submitting the form. Can you please review the code and provide a solution to fix this problem ...

Automatically expand a child node in the tree menu

Hey there! I've got a tree menu that I'm working with. I'm looking to have a specific node in the tree menu automatically open by default. Essentially, I want a particular node to be open when the page is refreshed. For instance, in this e ...

Immediately after setting up a fresh Angular 13 project, addressing the npm ERR! regarding the missing peer dependency ajv@^6.9.1 requested by [email protected]

Recently, I initialized a fresh Angular project by running npx @angular/cli@13 new Ubisoft.SupplierPortalNext.Spa and then performed npm install, which resulted in the following warning: npm WARN [email protected] requires a peer of ajv@^6.9.1 but n ...

Encountered a compiling error when attempting to watch for changes on the ./folder directory using the

An unexpected exception occurred: NullError: method 'length' not found on null File: /usr/local/lib/node_modules/sass/sass.dart.js Line: 8583:23 Function: tF.eA File: /usr/local/lib/node_modules/sass/sass.dart.js Line: 8585:28 Function: tF. ...

Release date approaching, but not as anticipated

Utilizing Java servlet to construct a portion of HTML and sending it with a JsonPrimitive for display in a dialog box. Here is the code snippet: ja = new JsonPrimitive( "<a href='#' onclick='return showDueDateUpdateDialogue(" + invoice. ...

Do double forward-slash comments work in CSS code?

Is using a double-forward slash (//) for single-line comments in CSS considered reliable and supported by mainstream browsers and CSS interpreters according to the specification? ...

Tips for creating a tabbed form that easily glides between tabs

After coming across this form design, I am inspired to create something similar with a twist. Instead of having each tab display all content at once, I want the tabs to scroll from right to left, giving it a more animated effect. Can someone offer suggesti ...

Navigating with Angular 4's router and popping up modals

I have an Angular 4 SPA application that utilizes the Angular router. I am looking to create a hyperlink that will open a component in a new dialog using Bootstrap 4. I am able to open modal dialogs from a function already. My question is, how can I achi ...

Tips for displaying an HTML page using JavaScript

In my project, I am working with an .html file (File X) that needs to immediately open another .html file (File Y) based on a certain value. Could someone provide guidance on the best way to achieve this using JavaScript? Additionally, I would like the pa ...

When scrubbing through videos, Chrome consistently throws a MEDIA_ERR_DECODE error

Encountering a MEDIA_ERR_DECODE error while scrubbing in Chrome on two different PCs. One PC runs Windows 7 with Chrome version 26, and the other runs Windows 8 with Chrome version 27. This issue occurs across all videos. When attempting to scrub on the v ...

Utilizing CSS attr() for setting the width dimension

I'm having some trouble setting the width of an element using attr() in CSS. Chrome keeps giving me an "invalid property value" error and I can't figure out what's causing it. What I'm attempting to do is use the attribute "prog" as th ...

Using JQuery's onChange event triggers actions based on the current values of input fields. However, in some

I am currently tackling an issue with a module that is designed to automatically select the only valid value from a Multi- or Single selection field when there is just one valid option and an empty choice available. Initially, everything was working smoot ...

React default class name ... Before conditional operator takes effect

import './App.css' import {Button} from "antd" <Button shape="round" className={ istrue === "itstrue" ? "trueclass" : "trueclass active" } > TEXT </Button> ...

What is the proper way to set up state with $stateProvider based on the folder organization in Angular JS?

I am currently utilizing $stateProvider for my routing needs. Below is an overview of my folder structure: app -css -style.css -js -app.js views -home.html -list.html index.html The state code I have implemented is as follows: app.config ...

Issues arise with the escape key functionality when attempting to close an Angular modal

I have a component called Escrituracao that handles a client's billing information. It utilizes a mat-table to display all the necessary data. When creating a new bill, a modal window, known as CadastrarLancamentoComponent, is opened: openModalLancame ...