ng-repeat过滤器空值不显示 [英] ng-repeat filter null value not displaying

查看:190
本文介绍了ng-repeat过滤器空值不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



pre $ c $ ng $ repeat =p in foo | filter:filter2

其中filter2是

<$ (p){
如果(p.state === null){
return p.state;
} else {
return;
}
};

这里是我正在说的一个plnkr: http://plnkr.co/edit/cj3v1fuBu6sHVI7A4qlq?p=preview



过滤器的工作原理是什么,但一个空,但我只想空显示。我可以尝试将所有空值更改为默认值字符串以外的空值?是否有任何地方可以让过滤器使用空值?

解决方案

我认为唯一错误的是您需要返回整个对象。

基于Brad的评论,我去检查了文档,他是对的。我的代码示例工作的唯一原因是它返回了一个'​​truthy'值。

我修改了下面的代码示例,将其考虑在内。



以下是过滤器:

$ $ $ $ $ $ $ $ $ scope.filter2 = function(p){
if(p.state === null){
return true;
} else {
return;
}
};

以下是文档中的相关部分:


函数(actual,expected):该函数将被赋予对象值和谓词值进行比较,如果该项目应包含在过滤结果中,则返回true。 b $ b

plunkr

Why won't angular display values that are null when I apply:

ng-repeat="p in foo | filter: filter2"

where filter2 is

 $scope.filter2 = function(p){
    if (p.state === null){
        return p.state;
    } else{
        return;
    }
};

here's a plnkr with what I'm saying: http://plnkr.co/edit/cj3v1fuBu6sHVI7A4qlq?p=preview

The filter works when it's anything but a null but I'm trying to only the null ones to display. I could try changing all the null values to a default value string other than a null? Is there anywhere to get the filter to work with nulls?

解决方案

I think the only thing wrong was that you needed to return the entire object.

Based on Brad's comment below, I went and checked the docs and he's right. The only reason my code sample works is because it's returning a 'truthy' value.

I've modified my code example below to take that into account.

Here's the filter:

$scope.filter2 = function(p){
    if (p.state === null){
        return true;
    } else{
        return;
    }
};

Here is the relevant section in the docs:

function(actual, expected): The function will be given the object value and the predicate value to compare and should return true if the item should be included in filtered result.

plunkr

这篇关于ng-repeat过滤器空值不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆