Maureen Moore

Maureen Moore

  • NA
  • 206
  • 0

Getter not returning the right values

Jul 25 2020 9:38 AM
I am trying to sum up all of my sub totals and return a grand total but I get wild values and I used to get the error: Expression has changed after it was checked.
When I use
  1. import { ChangeDetectionStrategy } from '@angular/core';  
  2. @Component({  
  3. changeDetection: ChangeDetectionStrategy.OnPush  
  4. })  
it suppresses the expression has changed error but I still get wild values for the grand total.
This is in my component:
  1. totals : number[ ] = [ ];  
  2.   
  3. get grandTotal() {  
  4.   
  5. let i;  
  6. let sub_total = 0;  
  7. let grand_total = 0;  
  8. sub_total = this.product_price * this.quantity;  
  9.   
  10. if (typeof this.product_price === "undefined") {  
  11. return 0;  
  12. else {  
  13. this.totals.push(sub_total);  
  14. for (i = 0; i < this.totals.length; i++) {  
  15. grand_total += this.totals[i];  
  16. }  
  17. return grand_total;  
  18. }  
  19. }  
This is in my HTML;
  1. <div>Grand Total {{ grandTotal }}</div>  


Answers (3)