Angularjs的foreach只返回一个对象 [英] Angularjs foreach only returns one object
问题描述
这可能是一些简单,我俯瞰它。但我按类别建立了一个过滤器,一旦用户点击一个类别它更新一个范围(我的实例$ scope.productStuff),并相应显示的对象。我的问题是,当我点击它给我回到我的控制台中的多张对象的类别。然后我看着DOM和那只能说明一个对象(和它的最后一个对象),而不是一切都在我的控制台中的对象。
这里是我的功能:
$ scope.update =功能(VAL){
angular.forEach($ scope.productStuff,功能(项目){
如果(item.s2 === val.toUpperCase()){
$ scope.productStuff = [项目];
}
});
}
下面是我的工厂,是越来越页面加载
数据 dataFactory.getProducts()。然后(功能(RES){
$ scope.productStuff = res.data;
$ scope.loading = FALSE;
});
所以我的问题是为什么会显示在控制台DOM和多个对象一个对象?如何把项目上的$ scope.productStuff?
$ scope.update =功能(VAL){
//创建一个空数组
VAR东西= [];
angular.forEach($ scope.productStuff,功能(项目){
如果(item.s2 === val.toUpperCase()){
当条件满足时推//我们的阵列(过滤器)
stuff.push(项目);
}
});
// $ scope.productStuff现在包含所有过滤项目
$ scope.productStuff =东西;
}
This could be something simple and I'm overlooking it. But I'm building out a filter by categories and once a user clicks a category it updates a scope (my instance $scope.productStuff) and display the objects accordingly. My problem is when I click the category it gives me back the mulitple objects in my console. Then I look at the dom and it only shows one object (and it's the last object) instead all the objects that are in my console. Here is my function:
$scope.update = function(val) {
angular.forEach($scope.productStuff, function(item){
if( item.s2 === val.toUpperCase()){
$scope.productStuff = [item];
}
});
}
Here is my factory that's getting the data on page load
dataFactory.getProducts().then(function(res){
$scope.productStuff = res.data;
$scope.loading = false;
});
So my question is why is it displaying one object in the dom and multiple objects in the console and how do I put the items on the $scope.productStuff?
$scope.update = function(val) {
// Create an empty array
var stuff = [];
angular.forEach($scope.productStuff, function(item){
if( item.s2 === val.toUpperCase() ){
// push to our array when condition is met (filter)
stuff.push(item);
}
});
// $scope.productStuff now contains all the filtered items
$scope.productStuff = stuff;
}
这篇关于Angularjs的foreach只返回一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!