How do I automatically update the value of a variable when a data is changed in ionic 3?

Aashan Ghimire

I am trying to get total price of the orders made that I have stored in an array. The code looks something like this.

import { Component } from '@angular/core';
import { Config } from '../config';
var ordersArray = [

];
@Component({
  selector: 'orders-page',
  templateUrl : 'orders.html'
})

export class Orders{
  public orders = ordersArray;
  public config = new Config;
  public addOrder(item){
    ordersArray.push(item);
  }
  public clearOrders(){
    var length = this.orders.length;
    for(let i = 0; i < length; i++){
      ordersArray.pop();
    }
  }
  public total = this.getPrice();
  public getPrice(){
    let price = 0;
    for(let order of ordersArray){
      price = (parseInt(order.item.price) * parseInt(order.quantity) ) + price;
    }
    return price;
  }
  public clearOrder(order){
    for(let i = 0;i < this.orders.length;i++){
      if(order.title == this.orders[i].title){
        ordersArray.splice(i,1);
      }
    }
  }
}

And the template, I have {{total}}. Now, when I remove an element from the ordersArray, I want the {{total}} to automatically update. How do I do that ?

Saurabh Agrawal

Call getPrice() after updating the ordersArray, So call getPrice() inside the clearOrder()

Try this

public clearOrder(order){
    for(let i = 0;i < this.orders.length;i++){
      if(order.title == this.orders[i].title){
        ordersArray.splice(i,1);
      }
    }
    this.total = this.getPrice(); 
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I automatically update cells to match values from another sheet when one cell is changed using data validation?

From Dev

Update vue.js data automatically when binded input field's value is changed by JavaScript

From Dev

Update vue.js data automatically when binded input field's value is changed by JavaScript

From Dev

Update variable when data in databases has changed

From Java

Update scope value when service data is changed

From Dev

How do I update data bindings once a variable changes value in AngularJS?

From Dev

how to update cache only when data is changed?

From Dev

How do I update the variables changed in a for loop?

From Dev

How to make a listbox automatically update when the source (list) is changed

From Dev

How to update data on server via AJAX when ng-model input value is changed

From Dev

Why is the value of a variable changed when I did not assign a new value to it?

From Dev

How do I get changed Value?

From Dev

How do I automatically update a foreign key

From Dev

How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

From Dev

How do I check when an input is changed?

From Dev

Storyboard: How do I let Xcode automatically update frames when I change layout constraints?

From Dev

My variable value changed, how to update my function accordingly?

From Dev

How do I retain the value of a for loop variable when the loop is over?

From Dev

"<<" inserts a pointer to a variable when I try to append the value of this variable, to another variable. How do I avoid this?

From Dev

How to update AngularJS view when the value has not changed?

From Dev

How do I correctly check form validity within a watcher when model data is changed outside of the form in angularjs?

From Dev

How to update element when data-* attribute changed

From Dev

How can I update protobuf file for client when server changed

From Dev

How can I make colores to be changed according to the value of scope variable?

From Dev

How do I output a variable changed in a void method in Java

From Dev

How to update SharedPreferences variable automatically?

From Dev

How do I know if a value of an element inside an array was changed?

From Dev

How do I update a value in an AngularJS (with TypeScript) controller when a value on an injected service updates

From Dev

How do I update multiple pages automatically in html?

Related Related

  1. 1

    How do I automatically update cells to match values from another sheet when one cell is changed using data validation?

  2. 2

    Update vue.js data automatically when binded input field's value is changed by JavaScript

  3. 3

    Update vue.js data automatically when binded input field's value is changed by JavaScript

  4. 4

    Update variable when data in databases has changed

  5. 5

    Update scope value when service data is changed

  6. 6

    How do I update data bindings once a variable changes value in AngularJS?

  7. 7

    how to update cache only when data is changed?

  8. 8

    How do I update the variables changed in a for loop?

  9. 9

    How to make a listbox automatically update when the source (list) is changed

  10. 10

    How to update data on server via AJAX when ng-model input value is changed

  11. 11

    Why is the value of a variable changed when I did not assign a new value to it?

  12. 12

    How do I get changed Value?

  13. 13

    How do I automatically update a foreign key

  14. 14

    How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

  15. 15

    How do I check when an input is changed?

  16. 16

    Storyboard: How do I let Xcode automatically update frames when I change layout constraints?

  17. 17

    My variable value changed, how to update my function accordingly?

  18. 18

    How do I retain the value of a for loop variable when the loop is over?

  19. 19

    "<<" inserts a pointer to a variable when I try to append the value of this variable, to another variable. How do I avoid this?

  20. 20

    How to update AngularJS view when the value has not changed?

  21. 21

    How do I correctly check form validity within a watcher when model data is changed outside of the form in angularjs?

  22. 22

    How to update element when data-* attribute changed

  23. 23

    How can I update protobuf file for client when server changed

  24. 24

    How can I make colores to be changed according to the value of scope variable?

  25. 25

    How do I output a variable changed in a void method in Java

  26. 26

    How to update SharedPreferences variable automatically?

  27. 27

    How do I know if a value of an element inside an array was changed?

  28. 28

    How do I update a value in an AngularJS (with TypeScript) controller when a value on an injected service updates

  29. 29

    How do I update multiple pages automatically in html?

HotTag

Archive