Search code examples
angularbuttoninputngfor

how to get input number value when button is clicked


I'm not sure how I can get the value of the button... this is my code

<p *ngFor = "let product of ProductsDetails; let i = index">
   <input type="number" value={{cartProducts[i].amount}} class="quantInput"> 
  <button mat-raised-button color="primary" class="changeBTN">change</button>
</p>

note: the amount of buttons & inputs is dynamic so I can't have a variable that will hold the input's value or I just don't know how to do it


Solution

  •    <p *ngFor = "let product of ProductsDetails; let i = index">
         <input type="number" [(ngModel)]="values[i]" value={{cartProducts[i].amount}} class="quantInput"> 
         <button mat-raised-button color="primary" class="changeBTN" (click)="doSomethingWithInputValue(i)" >change</button>
       </p>
    

    ts file

    values: any[];
    
    //initialize array when you have loaded productDetails
    ProductDetails.forEach((productDetail) => {
         this.values.push({});
    });
    
    
    public doSomethingWithInputValue(index){
    
        const currentInputValue = this.values[index];
        ...do everything you want with this value here
        }