在一个Web应用程序中,我有两个包含一些元素的框。我还有一个过滤器(由选择组成)。开始时,所有过滤器均已选中,并且可以正常工作。但是,当我尝试在第一个列表中什么都没有找到但第二个列表中有一些数据的组合时,出现了错误。这是我在控制器中调用服务时的情况:
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
我认为这angular.forEach
给了您一个范围界定的问题。请尝试以下代码。
if(data && data.people.length > 0){ // If i have some response form serve
var tmp = {};
var lastName = null;
for(var i = 0; i < data.people.length; i++){
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 && tmp.products && tmp.products.length > 0)
$scope.people.push(tmp);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句