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

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

问题描述

为什么我申请时angular不会显示为空的值:

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

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

filter2 在哪里

where filter2 is

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

这是一个 plnkr 和我所说的:http://plnkr.co/edit/cj3v1fuBu6sHVI7A4qlq?p=预览

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.

根据 Brad 在下面的评论,我去检查了文档,他是对的.我的代码示例有效的唯一原因是它返回了一个真实"值.

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.

这里是过滤器:

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

这是文档中的相关部分:

Here is the relevant section in the docs:

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

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天全站免登陆