在一个Ionic应用程序中,我有一个(隐藏的)选项卡,它的列表中填充了ng-重复:
<!-- ng-repeat that gets its items through a function -->
<ion-item ng-repeat="product in getProducts()">
//The function that provides the list back (+filter in my real code)
$scope.getProducts = function () {
console.log('In getProducts');
return $scope.list;
};
所有操作都很好,只是我注意到这个getProducts函数调用太多了,即使我已经离开了视图。我不知道是什么原因造成的,但我能够很容易地在Plnkr:https://embed.plnkr.co/BspdtQ/中复制这一结果。
要查看问题:在home选项卡中,单击“add”按钮,该按钮将导航到另一个选项卡。在控制台中,您将看到它正在按预期的方式触发函数。现在单击其他选项卡,您将看到它继续处理相同的代码,而不再在视图中。
是什么导致ng-重复刷新?我在这里错过了什么?
更新
作为解决办法,我禁用了特定视图的缓存,这样范围内的所有项都会被销毁,因此不再被处理。
发布于 2016-11-09 01:57:37
我认为ng-repeat
试图在每个摘要中评估产品,看看是否有变化。因此,在您的例子中,getProduct()
被多次调用。
如果您想要过滤您的产品,您可以创建一个自定义角度过滤器,并使用filter
内置指令在ng-repeat
。
<ion-item ng-repeat="product in products | filterProducts">
<h2>{{product.name}}</h2>
</ion-item>
JS
app.filter('filterProducts', function () {
return function (product) {
// your filter function
return product;
};
});
https://stackoverflow.com/questions/40495532
复制