我有一个基于此 example 的下表(尝试使用“搜索”输入)
<tr data-ng-repeat="product in filteredArray=(products | filter:findProducts)">
<td>{{$index+1}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
</tr>
如您所见,filteredArray 数组是动态的,因为该数组将根据过滤结果列出对象,而我需要做的是将这个 filteredArray 数组放入将 Controller 放入 forEach 方法中。
Controller
app.controller("controllerApp", function($scope){
$scope.products = [
{id:1, name: "product AAA", price: 12},
{id:2, name: "product AAB", price: 21},
{id:3, name: "product ABB", price: 32},
{id:4, name: "product ABC", price: 45},
{id:5, name: "product ACC", price: 34},
{id:6, name: "product ACA", price: 63},
{id:7, name: "product ACB", price: 47},
{id:8, name: "product BBA", price: 87},
{id:9, name: "product BBB", price: 59},
{id:10, name: "product BBC", price: 43},
{id:11, name: "product CCA", price: 76},
{id:12, name: "product CCB", price: 44}
];
// I can't get the 'filteredArray' from the view.
angular.forEach(filteredArray, function(value, key){
console.log("object from filtered array: "+value)
})
});
有什么想法吗?
最佳答案
在 Controller 中,您应该$watch
查找由ng-model
修改的findProducts
。在 $watch
处理程序中,您可以更新 filteredArray
。在您使用 forEach
编写的代码中,状态只会在 Controller 初始化时运行一次,而这不是您想要的。
关于javascript - AngularJS:从过滤器获取动态数组,然后将其用于 forEach 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193533/