简体   繁体   English

我如何在angular.forEach迭代期间推送新行?

[英]How do i push new row during angular.forEach iteration?

i have service response as below and i want to push a new row in the response and bind the new row in my View. 我有以下服务响应,我想在响应中推送新行并在我的View中绑定新行。 How do i do this with angular.forEach? 我该如何使用angular.forEach?

Service Response 服务回应

"message":
{
   "student" [

     {
       "namee" : "student1", 
       "oDate" : "03/03/2016",
       "oTime" :  "5:00 PM"
     },
     {
       "namee" : "student2", 
       "oDate" : "03/03/2016",
       "oTime" :  "6:00 PM"
},{
       "namee" : "student3", 
       "oDate" : "03/03/2016",
       "oTime" :  "7:00 PM"
} ]        
     }

}     

angular.forEach(student, function(a))
{
var sessionDate = a.oDate + a.oTime;
this.push(sessionDate);
}

This push statement is not working for me. 此推送声明对我不起作用。 I am fine with to write it in a new array too. 我也可以将其写入新数组中。 How do i acheive my requirement? 我如何达到我的要求?

If i understand your question correctly, what you are trying to do is add session date to each 'object' within student. 如果我正确理解了您的问题,那么您要尝试的是将会话日期添加到学生中的每个“对象”。

Try 尝试

angular.forEach(student, function(a) {
    sessionDate = a.oDate + a.oTime;
    a.sessionDate = sessionDate;
});

Here is a plunkr with example http://plnkr.co/edit/MzdfefKbE3vZYnbt0Tm5?p=preview 这是一个带有示例http://plnkr.co/edit/MzdfefKbE3vZYnbt0Tm5?p=preview的插件

Remember... push is for adding an object to an array. 记住... push是用于将对象添加到数组的。 In this case you are adding a key/value to a specific object within the array. 在这种情况下,您要将键/值添加到数组中的特定对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM