角度自定义过滤器返回错误

时间:2016-03-12 20:07:55

标签: javascript angularjs

我为我的表达式构建了一个自定义过滤器。它以某种方式工作。无论如何,我一直得到一个错误,我无法弄清楚为什么? 错误:“TypeError:无法读取属性'split'为null”

这是我的过滤器:

LeadApp.filter("q_Filter", function () {
    return function (input, splitChar, index) {
        if (index === 1) {
            q_a = input.split(splitChar);
            return q_a[1];
        }
        else {
            q_a = input.split(splitChar);
            return q_a[0];
        }
    };
});

以下是观点:

 <tr ng-repeat="select in lead_selection" ng-show="select.length && $index > 4">
    <td>
       <i class="fa fa-circle-thin"></i>
       <strong>{{ select | q_Filter:' | ':0 | uppercase }}</strong> <br />
       <p class="p-l-1">{{ select | q_Filter:' | ':1 }}</p>
    </td>
 </tr>

2 个答案:

答案 0 :(得分:2)

您应该在input输入值之前检查null是否为split

显示此错误是因为您inputnullempty

LeadApp.filter("q_Filter", function () {
    return function (input, splitChar, index) {

        if(!input) { 
           return '';
        }

        if (index === 1) {
            q_a = input.split(splitChar);
            return q_a[1];
        }
        else {
            q_a = input.split(splitChar);
            return q_a[0];
        }
    };
});

答案 1 :(得分:0)

在调用

之前,只需检查输入必须具有某些值的输入
input.split(splitChar)

还有一件事要确保输入包含splitChar