AngularJs 过滤器与控制器内部过滤器的性能
AngularJs Performance of Filter vs Filter inside Controller
如标题所说,过滤 ng-repeat
列表的最佳方法是什么?要使用自定义(或现有).filter()
或在我的控制器中创建自定义过滤器功能?
我之所以这么问是因为 .filter()
被调用了多次(每次都有一个 dygest 循环 according to this answer)。
所以我担心性能问题。如果我在 ng-repeat 中有一个巨大的列表要使用并且需要过滤它,那么在使用 .filter()
时它不会产生巨大的影响吗?
另一方面,当在控制器中使用自定义过滤器功能时,我只会在需要时过滤列表,并且只过滤一次。我对这部分的控制比较好
这是一个正确的逻辑吗?或者有没有其他方法可以优化过滤器的使用?
实际做到这一点的最佳方法是在 "directive" 中构建。如图所示(Ill Post 多个示例。):
app.controller('MainCtrl', function($scope) {
$scope.Items= [
{ id: 1, color: 'lightblue' },
{ id: 2, color: 'blue' }
];
});
div ng-repeat="item in Items| filter: { color: 'blue' }">
或者您可以使用
函数
app.controller('MainCtrl', function($scope) {
$scope.friends= [
{ id: 1, name: 'John' },
{ id: 2, name: 'Richard' }
];
$scope.searchText= function (item) {
return item === 'John';
}
});
<div ng-repeat="friend in friends | filter: searchText">
编辑2
所以对于嵌套数组...
app.controller('MainCtrl', function($scope) {
$scope.colors= [
{color:['blue','green','red']}
];
});
那么你会做...
<div ng-repeat="color in colors">
<div ng-repeat="uniqueColor in color">
{{uniqueColor}}
</div>
</div>
在这里你甚至不需要过滤器..我喜欢称之为 "nested ng-repeat" :D.
Here 是 fiddle 为了更好地演示..
EDIT3
现在过滤 ng-repeat..
<div ng-repeat="color in colors">
<div ng-repeat="uniqueColor in color | filter: 'Blue'">
{{uniqueColor}}
</div>
</div>
Fiddle 代表 here..
EDIT4
你可以像我读的那样使用:
app.filter("filterName", function(){
return function(obj){
return "Apple.inc"
}
})
我相信你是在问这样的问题? (查看日志)
https://plnkr.co/edit/B0WSa11DWcCaImEoexVh?p=preview
所以就像您链接的 post(链接到 fiddle)它确实会在 ng-filter
中触发过滤器两次,但是如果您在控制器中进行排序,它将只被击中一次
编辑
我的建议是,如果您有一个需要排序的大对象是:
后端排序(如果可能的话)
使用控制器内部的函数排序
(创建一个自定义过滤器,第一次过滤时会跳过
return 原来的? :I )
如标题所说,过滤 ng-repeat
列表的最佳方法是什么?要使用自定义(或现有).filter()
或在我的控制器中创建自定义过滤器功能?
我之所以这么问是因为 .filter()
被调用了多次(每次都有一个 dygest 循环 according to this answer)。
所以我担心性能问题。如果我在 ng-repeat 中有一个巨大的列表要使用并且需要过滤它,那么在使用 .filter()
时它不会产生巨大的影响吗?
另一方面,当在控制器中使用自定义过滤器功能时,我只会在需要时过滤列表,并且只过滤一次。我对这部分的控制比较好
这是一个正确的逻辑吗?或者有没有其他方法可以优化过滤器的使用?
实际做到这一点的最佳方法是在 "directive" 中构建。如图所示(Ill Post 多个示例。):
app.controller('MainCtrl', function($scope) {
$scope.Items= [
{ id: 1, color: 'lightblue' },
{ id: 2, color: 'blue' }
];
});
div ng-repeat="item in Items| filter: { color: 'blue' }">
或者您可以使用
函数
app.controller('MainCtrl', function($scope) {
$scope.friends= [
{ id: 1, name: 'John' },
{ id: 2, name: 'Richard' }
];
$scope.searchText= function (item) {
return item === 'John';
}
});
<div ng-repeat="friend in friends | filter: searchText">
编辑2
所以对于嵌套数组...
app.controller('MainCtrl', function($scope) {
$scope.colors= [
{color:['blue','green','red']}
];
});
那么你会做...
<div ng-repeat="color in colors">
<div ng-repeat="uniqueColor in color">
{{uniqueColor}}
</div>
</div>
在这里你甚至不需要过滤器..我喜欢称之为 "nested ng-repeat" :D.
Here 是 fiddle 为了更好地演示..
EDIT3
现在过滤 ng-repeat..
<div ng-repeat="color in colors">
<div ng-repeat="uniqueColor in color | filter: 'Blue'">
{{uniqueColor}}
</div>
</div>
Fiddle 代表 here..
EDIT4
你可以像我读的那样使用:
app.filter("filterName", function(){
return function(obj){
return "Apple.inc"
}
})
我相信你是在问这样的问题? (查看日志)
https://plnkr.co/edit/B0WSa11DWcCaImEoexVh?p=preview
所以就像您链接的 post(链接到 fiddle)它确实会在 ng-filter
中触发过滤器两次,但是如果您在控制器中进行排序,它将只被击中一次
编辑
我的建议是,如果您有一个需要排序的大对象是:
后端排序(如果可能的话)使用控制器内部的函数排序
(创建一个自定义过滤器,第一次过滤时会跳过 return 原来的? :I )