Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angularjs中添加项目成本?_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

如何在Angularjs中添加项目成本?

如何在Angularjs中添加项目成本?,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我想添加工作项成本,但它会显示原始值 例如: item[1].cost =2 ,item[2].cost = 2 .. 当我添加第三个项目[3]时,成本=8它是Total=228。我要加和,怎么做?我要总计=12 addWorkItem() { this.current_job.work_items.push(this.current_workitem); this.Total = 0; for (var i = 0, len = this.current_job.

我想添加工作项成本,但它会显示原始值

例如:

item[1].cost =2 ,item[2].cost = 2 ..
当我添加第三个
项目[3]时,成本=8
它是
Total=228
。我要加和,怎么做?我要总计
=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 = {};

  }
addWorkItem(){
this.current\u job.work\u items.push(this.current\u workitem);
这个。总数=0;
对于(变量i=0,len=this.current_job.work_items.length;i

这是因为您是在串联数字,而不是添加

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 = {};

}
addWorkItem(){
this.current\u job.work\u items.push(this.current\u workitem);
这个。总数=0;
对于(变量i=0,len=this.current_job.work_items.length;i
如果看不到更多的代码,很难说,但看起来您应该在此调用parseFloat。当前作业。工作项[i]。计算此项时的成本。总计什么不起作用?您得到的总计值是多少?您还可以对输入使用
type=“number”
,这将为您将它们转换为数字,并避免解析可能返回
NaN
的浮点值。