Search code examples
angularjsangularjs-directiveangularjs-scope

How to add item cost in Angularjs?


I want to add work item cost but its show original value.

Example:

item[1].cost =2 ,item[2].cost = 2 ..

When I add 3rd item[3].cost = 8 it's Total = 228. I want add SUM, how to do this? I want total =12.

addWorkItem() {

    this.current_job.work_items.push(this.current_workitem);
    this.Total = 0;

    for (var i = 0, len = this.current_job.work_items.length; i < len; i++) {
      this.Total += this.current_job.work_items[i].cost;
    }
  this.current_workitem = {};

  }

See picture


Solution

  • That's because you're concatenation the numbers, not adding.

    addWorkItem() {
      this.current_job.work_items.push(this.current_workitem);
      this.Total = 0;
    
      for (var i = 0, len = this.current_job.work_items.length; i < len; i++) {
        this.Total += parseFloat(this.current_job.work_items[i].cost);
      }
      this.current_workitem = {};
    
    }