【问题标题】:Angular custom filter returning error角度自定义过滤器返回错误
【发布时间】:2016-03-13 04:31:03
【问题描述】:

我为我的表达式构建了一个自定义过滤器。它以某种方式起作用。无论如何,我不断收到一个我无法弄清楚为什么的错误? 错误:“TypeError:无法读取属性‘split’ of 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>

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

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

    显示此错误是因为您的 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];
            }
        };
    });
    

    【讨论】:

      【解决方案2】:

      在调用之前检查输入必须有一些值

      input.split(splitChar)
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-15
        • 1970-01-01
        • 2020-07-27
        • 2019-02-16
        • 2015-04-17
        • 2013-12-15
        • 1970-01-01
        • 2017-05-02
        相关资源
        最近更新 更多