Angular无法读取null属性

时间:2016-03-23 10:53:09

标签: javascript angularjs service undefined

在网络应用中,我有两个包含一些元素的框。我也有一个过滤器(由选择组成)。在开始时,过滤器都被选中,并且它可以工作。但是当我尝试在第一个列表中找不到任何内容但在第二个列表中有一些数据的组合时,我收到了一个错误。这是我在控制器中调用服务的时候:

if(data){ // If i have some response form serve

 var tmp = null;
 var lastName = null;

 angular.forEach(data.people, function(ppl){
 if(ppl.lastName != lastName){
    if (lastName != null){
        $scope.people.push(tmp);
    }
    // Clean the tmp
    tmp = {
           name : null,
           count : 0,
           products : []
     };
     // Update lastName
     lastName = ppl.lastName;
  }
   // Add the actual row in the ob
   tmp.name = lastName;
   tmp.count += ppl.personCount;
   tmp.products.push(ppl);
  });

  // Process lasts elements (if there are some) in the array
  if(tmp.products.length > 0){
    $scope.people.push(tmp);
  }

错误是:Cannot read property 'products' of null

我试过写:var tmp = [];而不是null,但它说Cannot read property 'products' of undefined

1 个答案:

答案 0 :(得分:0)

我认为colnames(df) <- sub('\\.[^.]+$', '', colnames(df)) colnames(df) #[1] "a.b.c" "d.e.f" "h.j.k" 正在给你一个范围问题。请尝试使用以下代码。

angular.forEach