Calculated sum correct in HTML but not saving in firebase?


#1

Hi

I am able to get the sum in the HTML correctly with following codes:

<ion-item type="text" formControlName="billValue" [(ngModel)]="retailerItemModel" name="billValue" ngDefaultControl >Total Bill Value Rs. {{ totalValue(retailerItemModel) || 0 | number: '1.2-2' }}
          </ion-item>

I am using Firebase to save data with AngularFire service provider:

saveBill(billValue: any =[], ): firebase.Promise<any>{
    return this.afDatabase.list(`userProfile/${this.userId}/billList/`).push({
     
      billValue,      
      
    })
  }

The problem is after saving the firebase data shows array:

sum problem

I want the sum of selected values to show not the array… Is there any way to do the calculation in HTML itself or controller for selected value… Following is the controller formula:

totalValue(retailerItemModel: any=[]){
    let total = 0;
    retailerItemModel.forEach((item) => {
      total += Number(item);
    })
    return total;
  }

#2

I don’t know what’s going on with firebase, but don’t interpolate totalValue(), it’s wasteful. Instead call it only when one of its inputs has changed and store the result in a separate controller property that is then referenced from the template. Much more efficient.


#3

Thanks Rapropos. But even if I refer to ionChange=“selectedItems()” and calculate total with ‘for each’ method… The result is the same(Obviously)… I am not understanding the logic to connect the sum of array elements within the selectedItems that can save as ‘billValue’


#4

I solved it while using the same logic on Provider…


#5

Can you get the code or description what is provider here… can you send me full code of your project please