Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Angular 如何获取值​;从表中的列并将其放入数组中?有棱角的_Angular_Typescript - Fatal编程技术网

Angular 如何获取值​;从表中的列并将其放入数组中?有棱角的

Angular 如何获取值​;从表中的列并将其放入数组中?有棱角的,angular,typescript,Angular,Typescript,我有一个具有以下代码的组件(一些表): 预计下个月.component.html <table id="users"> <tr> <th>Number of month</th> <th>Total checking electrical units</th> <t

我有一个具有以下代码的组件(一些表):

预计下个月.component.html

        <table id="users">
            <tr>
                <th>Number of month</th>
                <th>Total checking electrical units</th>
                <th>Total assembly</th>
                <th>Total soldering</th>
                <th>Total packing</th>
                <th>Total working days in this month</th>
            </tr>
            <tr *ngFor="let sum of totalWorkPerMonth">
                <td>{{sum.number_of_month}}</td>
                <td>{{sum.total_checking_electrical_units}}</td>
                <td>{{sum.total_assembly}}</td>
                <td>{{sum.total_soldering}}</td>
                <td>{{sum.total_packing}}</td>
                <td>{{sum.total_working_days_in_this_month}}</td>
            </tr>
        </table>

月数
总检查电气装置
总成
全焊
总包装
本月总工作日数
{{sum.number{u月}
{{sum.total_checking_electronic_units}}
{{sum.total_assembly}
{{总和}
{{sum.total_packing}}
{{本月工作日总数}
目标是从total\u packing列中获取所有数据,并将其放入ts文件中的一个数组中,以便我进行验证。(这是一个具有int值的列)

有人知道怎么做吗?我尝试使用ngModel,但它没有真正起作用。。。可能我写得不正确,或者不可能得到所有的值​​以这种方式为数组类型的一个变量。
非常感谢您的帮助。谢谢

1。您可以使用map函数在.ts文件中的新数组中获取它

var result = this.totalWorkPerMonth.map(x => x.total_packing)

// apply your verification foreach item from your list:
result.foreach(packing => {
  // do something
}
2.另一种方法是对属性应用方法:

<td>{{validateField(sum.total_packing)}}</td>
3.第三种解决方案,我个人认为是最好的:在得到数据后,在呈现结果之前进行验证

假设您的组件中有一个方法
getData()
,并且您希望对此字段应用验证:

getData() {
   service.getData().subscribe(data => {
      data.foreach(row => {
         // do your validation as in example
         if (row.total_packing< 0) {
           row.total_packing = 0;
         }
      }
      this.totalWorkPerMonth = data;
   });
}
getData(){
service.getData().subscribe(数据=>{
data.foreach(行=>{
//按照示例进行验证
if(第行总包装<0){
row.total_packing=0;
}
}
this.totalWorkPerMonth=数据;
});
}

正如您编写的那样,我在ts文件中编写了,我尝试执行cosole.log以查看数组是否确实已填充,但数组为空result=this.totalWorkPerMonth.map(x=>x.total_packing);checkButton(){console.log(this.result);}第一个方法中的
map
不是,而是方法。调用的方法相同,但用途完全不同。@MichaelD,是的,对不起,这里的map()是一个js方法,应用于数组的每个元素。
getData() {
   service.getData().subscribe(data => {
      data.foreach(row => {
         // do your validation as in example
         if (row.total_packing< 0) {
           row.total_packing = 0;
         }
      }
      this.totalWorkPerMonth = data;
   });
}