Activate only the button that has been selected

After developing two buttons that change the text when clicked, I encountered an issue where clicking on one button would cause all the others to change as well.

Is there a way to only change the text of the button that was clicked?

View DEMO

HTML Code Sample

<div class="Submitcomments" *ngFor="let c of data; let  i = index;">
    <div>
        <div class="row rowComments" style="margin-top: 11px;">
        </div>
        <div class="form-group">
            <textarea type="text" class="form-control AreaText" rows="2"></textarea>
        </div>
        <div class="row" style="margin-top: -20px; margin-left: 5px;">
            <button *ngIf="currentState=='pause'" class="btn reply" (click)="currentState='start' ; Execute(i)">Yes</button>
            <button *ngIf="currentState=='start'" class="btn reply1" (click)="currentState='pause'; Execute(i)">No</button>
        </div>
    </div>

Answer №1

It might be beneficial to extract the comments code into a separate component as it can simplify the template and reduce complexity. However, whether or not to do this is open to debate.

If you choose not to separate the comments as a component, you could instead maintain individual variables to store the data for each comment:

data = [
    {    
        ID:1,
        Name : "Name",
        currentState: "pause"
    },
    {    
        ID:2,
        Name : "Name2",
        currentState: "pause"
    },
       {    
        ID:3,
        Name : "Name3",
        currentState: "pause"
    },
       {    
        ID:4,
        Name : "Name4",
        currentState: "pause"
    },
  ];

For the HTML implementation, you can consider the following structure:

<div class="Submitcomments" *ngFor="let c of data; let  i = index;">
    <div>
        <div class="row rowComments" style="margin-top: 11px;">
        </div>
        <div class="form-group">
            <textarea type="text" class="form-control AreaText" rows="2"></textarea>
        </div>
        <div class="row" style="margin-top: -20px; margin-left: 5px;">
            <button *ngIf="c.currentState=='pause'" class="btn reply" (click)="c.currentState='start' ; Execute(i)">Yes</button>
            <button *ngIf="c.currentState=='start'" class="btn reply1" (click)="c.currentState='pause'; Execute(i)">No</button>
        </div>
    </div>
</div>

Alternatively, you could keep a separate array to manage the states if you prefer not to alter the original object array:

currentStates = [
    "pause",
    "pause",
    "pause",
    "pause"
]

   data = [
    {    
        ID:1,
        Name : "Name"
    },
    {    
        ID:2,
        Name : "Name2"
    },
       {    
        ID:3,
        Name : "Name3"
    },
       {    
        ID:4,
        Name : "Name4"
    },
  ];

Incorporate these states in the HTML like so:

<div class="Submitcomments" *ngFor="let c of data; let  i = index;">
    <div>
        <div class="row rowComments" style="margin-top: 11px;">
        </div>
        <div class="form-group">
            <textarea type="text" class="form-control AreaText" rows="2"></textarea>
        </div>
        <div class="row" style="margin-top: -20px; margin-left: 5px;">
            <button *ngIf="currentStates[i]=='pause'" class="btn reply" (click)="currentStates[i]='start' ; Execute(i)">Yes</button>
            <button *ngIf="currentStates[i]=='start'" class="btn reply1" (click)="currentStates[i]='pause'; Execute(i)">No</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

Retrieving Color Values from Stylesheet in TypeScript within an Angular 2 Application

Utilizing Angular2 and Angular Material for theming, my theme scss file contains: $primary: mat-palette($mat-teal-custom, 500, 400, 900); $accent: mat-palette($mat-grey4, 500, 200, 600); There is also an alternate theme later on in the file. Within one ...

Activate individual row Kendo UI Angular 2 grid mouse hover event

I understand that utilizing the template syntax allows me to utilize the standard angular 2 (mouseover) event listener in order to detect a mouse over event for a specific column. I am curious if there exists a method to listen for mouseover events for an ...

Using the Vuex Module Decorator: A guide on accessing the store from a module

Recently, I've been playing around with the vuex module decorator in order to maintain good TypeScript practices in my vuex stores. However, I'm facing issues when trying to access the store without encountering type definition problems. My setu ...

Navigating to a specific section upon clicking

Imagine a scenario where there is a landing page with a button. When the button is clicked, redirection to another page with multiple components occurs. Each component on this new page serves a different function. Additionally, the desired functionality in ...

Angular Material Cards do not expand to fill the entire row

I recently started using Angular Material and I'm attempting to create a page with cards containing photos. However, it seems that by default, the mat-cards stack vertically and do not fill out the space in the row to the right. I've experimented ...

Updating values in a mat-select component using the Angular patchValue method

Within my Angular 7 application, I am utilizing the mat-select component from Angular Material in a reactive form. This is how the view appears: <mat-form-field class="col-md-3" color="warn"> <mat-select placeholder="Selectionner la ...

Angular: Error encountered due to a SyntaxError caused by an unexpected token < appearing in JSON at position 0

Hey there, I need some assistance. I've created an Angular service but when I try to access data from my JSON file, I keep encountering errors. I've tried several solutions without success. Here is the code in app.component.ts: import {Comp ...

Setting ngModel to the month input control

I am trying to use ngModel to link a date object that contains the current month value and display the month and year on the input month calendar. However, even though the object is successfully bound to the control, the month/year does not appear. Any tip ...

Issue encountered when attempting to run the Angular 7 application using Node Js LTS version 16.13.2

I have set up an Angular 7 app using the following docker file with Node.js version 16.13.2 LTS FROM node:16.13.2 ENV context "" RUN mkdir -p /usr/src/ WORKDIR /usr/src/ COPY . /usr/src/ RUN npm install ENV context "" EXPOSE 3000 CMD [ ...

Tips for validating a reactive form with Bootstrap styling

Here is a simplified version of my code: <div class="tab " *ngIf="booking"> <div class="confirmation-email card" *ngIf="showConfirmationEmailForm" id="confirmationEmail"> <div class=" ...

Deselect radio button in Angular 7 using Bootstrap 4 styling

Seeking assistance with using Angular 7 and Bootstrap 4 to create radio buttons that can toggle on and off. I came across a solution here. Check out my current method. Currently, I am able to toggle the buttons, however, I now face a new dilemma. I want e ...

The art of transferring an @Input string from one component to another

I have successfully passed data from one component's TypeScript file to another component's HTML file. Here is the code snippet: export class TestComponent { @Component({ selector: 'app-data', }) @Input() data : string; } ...

Unexpected behavior: Axios post still enters catch block despite successful completion of Rest API call

Having an issue with my axios post request not returning the value from the API in a non-success scenario (401 error). It works fine for successful scenarios. When using Postman to test the output of my reset password API by providing an incorrect current ...

Trustpilot: The function window.Trustpilot.loadFromElement does not exist in Safari

I am looking to integrate 3 TrustPilots into my Angular application. import { Component, Input, OnInit } from '@angular/core'; declare global { interface Window { Trustpilot: any; } } window.Trustpilot = window.Trustpilot || {}; @Component ...

Utilizing Material-UI with MobileDialog HOC in TypeScript: A Beginner's Guide

I'm running into an issue while trying to implement withMobileDialog in my TypeScript code. Below is the snippet of my code, inspired by a code example from the official documentation. import withMobileDialog, { InjectedProps } from "@material-ui/co ...

The powerful combination of Vuetify, Inertia, and Typescript creates a

Currently, I am working with Vuetify and Inertia while using Typescript. I am faced with the challenge of passing shared data to the items prop of a vuetify AutoComplete component: <v-autocomplete :items="$page.props.countries"></v-auto ...

Spring Boot being called from Angular App: Access-Control-Allow-Origin restriction not permitted

Having an issue with CORS while trying to connect my Angular 8 app to a Spring boot REST API. Despite researching online, I haven't been able to resolve the problem. http://localhost:4200 ---------------> http://localhost:8080 The CORS error mes ...

What is the best way to execute a method in a component for each iteration in an *ngFor loop in Angular 2

Is there a way to call a method for each iteration of *ngFor and pass the iterated variable as a parameter? For example: <li *ngFor="let element of componentModel | keys">{{element.key}}--{{element.value}}</li> Then, in the component, I have ...

Angular 2: Encounter with 504 Error (Gateway Timeout)

I am struggling with handling errors in my Angular 2 application. Whenever the backend server is offline, an uncaught error appears in the console: GET http://localhost:4200/api/public/index.php/data 504 (Gateway Timeout) This is how my http.get me ...

Show a few values as a string in Angular using Typescript

I am working with JSON data and I want to know how to display hobbies without including the word "all" in an Angular/TypeScript application. { "Name": "Mustermann1", "Vorname": "Max1", "maennlich& ...