TypeError:无法读取未定义的属性"split" [英] TypeError: Cannot read property 'split' of undefined

查看:126
本文介绍了TypeError:无法读取未定义的属性"split"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML文件.当我运行HTML页面时,它显示错误,无法读取未定义的属性" split"

This is my HTML file. When i am running the HTML page it is showing error,'Cannot read property 'split' of undefined'

<!DOCTYPE html>
      <html>
            <head>
                <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>

                <script>
                     var appModule=angular.module('appModule',[]);
                     appModule.filter('removeDashes',function(){
                         return function(text){
                             if(text!==undefined){
                                 return text.split('-').join(' ');
                             }
                         }
                     });
                     appModule.controller('someCTRL',function($scope){
                     });
                </script>
             </head>

             <body ng-app="appModule" ng-controller="someCTRL">
            <input type="text" model="someDashedText">
            <p>
                {{someDashedText | removeDashes}}
            </p>
        </body>
    </html>

推荐答案

if(text!==undefined){
                             return text.split('-').join(' ');
                         }

我认为以上条件应替换为以下代码

In my opinion above condition should be replaced with below code

if(text){
                             return text.split('-').join(' ');
                         }

此条件检查所有已定义的内容,即不为null且不为空的字符串.

This condition checks all i.e defined, not null and not empty string.

希望这会有所帮助.

这篇关于TypeError:无法读取未定义的属性"split"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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