Using Ionic 3 to create a list view that includes buttons linked to the items clicked

I need assistance with modifying the button icon in a list layout:

  <ion-list inset *ngFor="let audio of event.audios; let i = index">
      <ion-item>

        <div class="item-text-center item-text-wrap">  {{audio.fileName}} </div>

        <ion-buttons end>
          <button  end ion-button icon-only color="primary" (click)="playAudio(audio)">
            <ion-icon name="{{playButtonIcon}}"></ion-icon>
          </button>
          <button  end ion-button icon-only color="primary" (click)="deleteAudio(audio)">
            <ion-icon name="close"></ion-icon>
          </button>
        </ion-buttons>

      </ion-item>
  </ion-list>

The issue is that when I click on an item, every button in the list changes its icon. This behavior is not desired, as only the clicked item's icon should change.

How can I modify the button icon of the clicked item exclusively? My initial idea was to use an additional array to keep track of the playButtonIcon variable for each item. However, this approach does not seem optimal. Is there a recommended solution for this scenario?

Please note that while I can identify the clicked item, my data model lacks a playButtonIcon field. Therefore, I require an alternative method to access and update the button of the clicked item.

Answer №1

Have you considered implementing something like the following?

view.html

     <button  end ion-button icon-only color="primary" (click)="playAudio(audio)">
  <ion-icon name="{{playButtonIcon}}" *ngIf="typeOf audio.isPlaying == 'undefined'">{{Playbuttonicon}}</ion-icon>
  <ion-icon name="{{pauseButtonIcon}}" *ngIf="typeOf audio.isPlaying != 'undefined'">{{PauseButtonIcon}}</ion-icon>
</button>

component.ts

playAudio(audio){
//get the index of the current audio in the array 
 let idx = this.audios.indexOf(audio);
//make a new property called isPlaying and assign a boolean true
this.audios[idx]["isPlaying"] = true; 
}

Alternatively, you could loop through the array again and add an extra property called icon that changes on click.

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

Unlock the Power of Angular: Leveraging ViewEncapsulation.Native to Access HTML Elements

I am encountering an issue where I am receiving an error when trying to access an HTML element by ID. The problem arises when I attempt to access the classList upon a user clicking a button to apply a different style class to the element. The class list an ...

Shortcuts for $scope variables in AngularJS templates

Within my controller, I often set: $scope.currentThing.data For different parts of my template, sometimes I require currentThing.data.response[0].hello and other times currentThing.data.otherStuff[0].goodbye //or currentThing.data.anotherThing[0].goo ...

Can anyone assist me with creating a custom sorting pipe in Angular 2?

*ngFor="let match of virtual | groupby : 'gameid' I have this code snippet that uses a pipe to group by the 'gameid' field, which consists of numbers like 23342341. Now, I need help sorting this array in ascending order based on the g ...

Tips on obtaining data from ion-select and presenting it in a label

I'm having a problem with dynamic binding in my drop-down. I can't seem to display the default selected value in the label, and when I change the selection, the new value isn't showing up either. Take a look at my code below: <p>{{l ...

Is Angular capable of displaying a date within a specific timeframe using ng-show?

So I have a button that, when clicked, displays a list of data. I am adding a unique font awesome icon in there if the JSON key value exists. However, here lies the issue. The particular key happens to be a date. I need my ng-show function to check whether ...

Downloading PDF files on IOS while using Angular often results in the PDF opening in the same

I'm currently utilizing file-saver within my Angular application to retrieve a PDF generated from the backend. The library functions smoothly on desktop and Android devices, but I'm encountering issues with downloading files on iOS. Contrary to w ...

How to correct placeholder text display in the input of a Material UI text field

Currently, I am utilizing the material ui text field component. The issue at hand is that when the text field is in focus, the placeholder shifts to the top of the field. https://i.stack.imgur.com/P5flf.png I prefer for the placeholder to remain within ...

Using AngularJS to connect a REST search endpoint to $resource mapping

The ng-resource module in AngularJS provides a default set of resource actions which include 'get', 'save', 'query', 'remove', and 'delete'. { 'get': {method:'GET'}, ...

Using $anchorScroll in Angular to create an anchor link that links to the same page but with a style change seems to be ineffective

I have a simple Angular view that includes a menu. Each item in the menu serves as a link to a specific section of the same page. I am utilizing $anchorScroll to achieve this functionality and it is functioning correctly. However, I am encountering an issu ...

Showing the AngularFireList data on the HTML page following successful retrieval

Does anyone know how to display an AngularFireList on an HTML page? import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import {AngularFireAuth} from 'angularfire2/auth'; import {AngularF ...

$rootsScope.$watch not firing as expected

I am encountering an issue with a watch set in a service (shown in the code snippet below) that is not triggering as expected. Strangely, a similar watch set in a controller using $Scope.$watch is working perfectly when the object changes. What could be c ...

Angular 5 Dilemma: Exporting UI Components without Locating Template

My current project involves developing UI Components that will be used in various web projects within the company. Our plan is to publish these UI components as an npm package on our local repository, and so far, the publishing process has been successful. ...

What is the most efficient way to convert a JSON object into a URL search parameter using as few characters as possible?

Challenge: On my web app, users can adjust settings to create or edit generative artworks and then share their creations via a shortened link. The objective is to store all the data needed to replicate the artwork in the URL within 280 characters. Initia ...

Using Modal Functions in AngularJS Controller

I have been working on a project that utilizes the ui.bootstrap. As per the instructions from the tutorial I followed, my setup looks something like this: 'use strict'; angular.module('academiaUnitateApp') .controller('EntryCtr ...

The toggle-input component I implemented in React is not providing the desired level of accessibility

Having an accessibility issue with a toggle input while using VoiceOver on a Mac. The problem is that when I turn the toggle off, VoiceOver says it's on, and vice versa. How can I fix this so that VoiceOver accurately states whether the toggle is on o ...

Utilizing HTML5 to automatically refresh the page upon a change in geolocation

I have a web application built with AngularJS. One of the functionalities is activated only when the user is in close proximity to specific locations. To make this work, I can constantly refresh the page every 5 seconds and carry out calculations to dete ...

React's setState function failed to update the specified value within the set

In attempting to update the state values, I encountered an issue where the state did not get updated as expected. To troubleshoot, I included console logs at each line of code. handleFilter=(event)=> { console.log(this.state.answerStatus) // In ...

Issue: Generated JavaScript files not visible in Visual Studio when using TypeScript.Explanation: When working with

Is there a way to locate the JavaScript files generated from the TypeScript file in Visual Studio 2015? It seems that although the JavaScript files are present in File Explorer, they are not visible in the solution explorer. I attempted to add the _refer ...

AngularJS validation within a bootstrap state selector

Has anyone encountered issues with validating the Bootstrap country state picker using AngularJS? I am struggling to get it to work properly. Any suggestions or possible solutions? ...

Creating distinctive ng-form tags within a form using ng-repeat in Angular

I need help with creating a form that includes a table looping over a list of objects. Each object should have checkboxes for the user to check/uncheck attributes. The issue I am facing is setting the ng-model attribute on the checkboxes. This is what my ...