Retrieve the name from the accordion that was clicked

Hey there, I have a simple accordion that is based on an API called "names".

<div *ngFor="let item of showDirNames | async | filter: name; let i = index;">
   <button class="accordion" (click)="toggleAccordian($event, i)"> {{item.name}} </button>
   <div class="panel" *ngFor="let child of fileUploads | async | filter: name" hide="!item.isActive">
      <p *ngFor="let child of fileNames| async | filter: name"> {{child.name}} </p>
   </div>
</div>

This is how it appears:

DirName_1

  • file_name
  • file_name

DirName_2

  • file_name
  • file_name

When the user clicks on DirName_1, they will see a list of file names within that directory. How can I retrieve the name (for example DirName_1) from the user? I want to identify which accordion was clicked by the user in order to provide the corresponding file names from the server.

Answer №1

Make sure to pass the variable item.name into the function toggleAccordian. By the way, there seems to be a typo in the name of the function; it should be toggleAccordion:

<div *ngFor="let item of showDirNames | async | filter: name; let i = index;">
   <button class="accordion" (click)="toggleAccordian($event, i, item.name)"> {{item.name}} </button>
   <div class="panel" *ngFor="let child of fileUploads | async | filter: name" hide="!item.isActive">
      <p *ngFor="let child of fileNames| async | filter: name"> {{child.name}} </p>
   </div>
</div>

The correction for toggleAccordian has not been applied in this example. You will need to manually update every occurrence yourself if desired.

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

Utilize a module within a script in the continuous integration setup of a React application

I've created a module to verify if all the necessary environment variables are properly set in a React application. Here's a simple example of how it works: const getEnvironmentVariable = (key: string): string => { if (process.env[key]) { ...

Unable to install a specific commit of an angular library from GitHub using npm

While utilizing Angular 2.0.0-beta.15, I encountered the inability to upgrade it. Thus, I had to search for a specific commit from the ng2-dnd library on GitHub. Upon locating a compatible commit for version 2.0.0-beta.17: "ng2-dnd": "git://github.com/ak ...

`Is it possible to animate the rotation of an image within an input control?`

After coming across this helpful answer, I successfully added a rotating GIF image as an icon inside an INPUT control on its right side. Simply applying the "busy" class to my control did the trick, and now it looks like it's in progress. input.busy { ...

Encountering a JavaScript/TypeScript issue that reads "Unable to access property 'Items' as it is undefined"

I encountered an issue with Javascript where I'm receiving an error message stating "Cannot read property 'Items' of undefined". The this keyword is consistently showing as undefined in the Base class. How can this problem be resolved? Coul ...

Optionalize keys containing a specific character in their name

Imagine I have an object similar to the following: const obj = { a: 1, "b?": 2, "c?": 3 } The current type of this object is as follows: type Obj = { a: number; "b?": number; "c?": number; } Is there a ...

Displaying object properties in React and rendering them on the user interface

Within my React application, I am retrieving data from an API using the following code snippet: function PlayerPage() { interface PlayerDataType { id: number; handle: string; role: string; avatar: string; specialAbilities: null; s ...

What is the process of querying both a collection and a subcollection in Firebase using AngularFire?

I have a structure in my firebase database that looks like this: /profiles/{uid}/displayName /email /otherAttribues /roles/{roleName}/someAttribute /someOtherAttribute The reason ...

The ngx-incredible-scrollbar is malfunctioning

I encountered an issue when trying to include the "perfect-scrollbar" library in my project. I followed the installation and inclusion steps provided below: However, upon running the project, I'm facing the following error: Error: Unexpected token & ...

Error Encountered When Trying to Import Mocha for Typescript Unit Testing

Here's a snippet of code for testing a Typescript project using mocha chai. The test case is currently empty. import {KafkaConsumer} from '../infrastructure/delivery/kafka/kafka-consumer'; import {expect} from 'chai'; import {descr ...

Submitting a form using an anchor tag in Angular 8: A step-by-step guide

I have a question about how to submit form data using hidden input fields when a user clicks on an <a> tag. <form action="/submit/form/link"> <input type="hidden" [attr.value]="orderNumber.id" /> <input type="hidden" [attr.value]= ...

Is there a way to halt the automatic expansion of a mat-expansion-panel upon being clicked?

I have a specific requirement for managing the opening and closing of my mat-expansion-panel. In my implementation, I want to rely solely on the panel's [expanded] input property to control its state. To achieve this, I am using NGRX as my state manag ...

Using the HTTP Post method to retrieve a file object: a step-by-step guide

Is there a way to utilize a http POST request in order to retrieve a file object? Though the uploading of files to the server using the POST request seems successful and flawless, attempting to fetch the file results in an unusual response: console output ...

Trigger the browser to refresh translation files following the deployment

Our Angular/Ionic app utilizes the ngx-translate/core package for translations, and is hosted on Firebase. With each new build and deployment, Angular automatically creates a hash for our js files to ensure the browser fetches the latest version when chang ...

Top Tip for Preventing Angular Version Compatibility Issues

Here is an illustration that delves into my inquiry ----> Version Conflict The dilemma arises when my product has a dependency on a node package, which in turn relies on a specific version of Angular, denoted as version #y. However, my product itself dep ...

There is no index signature that accepts a parameter of type 'string' in the type '{ [key: string]: AbstractControl; }'

I'm currently tackling a challenge in my Angular project where I am creating a custom validator for a reactive form. However, I've encountered an error within the custom validators function that I am constructing. Below you will find the relevan ...

`Is there a way to repurpose generic type?`

For instance, I have a STRING type that is used in both the test and test2 functions within the test function. My code looks like this: type STRING = string const test = <A = STRING>() => { test2<A>("0") } const test2 = <B& ...

Set up an event listener for when geolocation permission is approved

My Setup: I've written some basic code snippet below: const onSuccess = () => { console.log('success'); } const onError = () => { console.log('error'); } navigator.geolocation.getCurrentPosition(onSuccess, onError) ...

Having trouble with the lodash find function in my Angular application

npm install lodash npm install @types/lodash ng serve import { find, upperCase } from 'lodash'; console.log(upperCase('test')); // 'TEST' console.log(find(items, ['id', id])) // TypeError: "Object(...)(...) is un ...

Exploring Angular localization using ng2-smart-table

Currently, I am utilizing ng2-smart-table. In my component.ts file, I am setting the column headings. settings = { actions: { add: false, edit: false, delete: false }, columns: { date: { title: 'Date' ...

Excessive repetition in the style of writing for a function

When it comes to TypeScript, a basic example of a function looks like this: let myAdd: (x: number, y: number) => number = function ( x: number, y: number ): number { return x + y; }; Why is there redundancy in this code? I'm having trouble g ...