Skip to content
Loading
  • +2
    I am looping through the ngFor directive so the quantity and price get summed up repeatedly. But when I take the function out of the for loop it doesn't have any access to the price and quantity variables.
    +2
  • +2
    When I take grandTotal out of the ngFor loop it does access the price and quantity variables after all but I am still getting wild values.
    +2
  • +2
    I added a pipe but the pipe doesn't work. This is what I have:
    1. @Pipe({name: 'grandtotal'})  
    2.   
    3. export class GrandTotalPipe implements PipeTransform {  
    4. sub_total = 0;  
    5. product_price = 0;  
    6. quantity = 0;  
    7. transform(product_price: number, quantity: number, sub_total: number) {  
    8. sub_total = product_price * quantity;  
    9. sub_total += sub_total;  
    10. return sub_total;  
    11. }  
    12. }  
    and in the HTML:
    1. <div>Grand Total {{product_price | grandtotal: quantity : sub_total}}div>  
    +2
  • +2
    The problem is that the price and quantity are getting submitted twice. Once when the quantity is increased or decreased and then again when the Add to Cart button calls the onSubmit function.
    +2
  • Jefferson S. Motta
    +2
    Without seeing your whole code is difficult but looks like onSubmit is double defined his $".action".
    +2
  • +2
    Thanks Jefferson. Where is the onSubmit function double-defined? I don't see it.
    +2
  • Jefferson S. Motta
    +2
    Me too.
     
    Show all parts of your code where onSubmit appears. 
     
    Best, 
     
    Jeff 
    +2
  • +2
    In the HTML I have
    1. <form *ngFor="let product of products" [formGroup]="myForm" (ngSubmit)="onSubmit(product.thumbnail, quantity, product.name, product.price )">  
    2.   
    3. <button type="submit" class="btnAddAction">Add to Cartbutton>  
    and in the controller, I call the onSubmit function.
    +2
  • +2
    I tried changing the button to type=button but then the form didn't submit at all.
    +2
  • Jefferson S. Motta
    +2
    Here are!
     
    you are using a loop with more than one button:
     
    +2