类型错误:无法读取未定义的属性“拆分”

新手上路,请多包涵

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

 <!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>

原文由 Abhishek 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 383
2 个回答
if(text!==undefined){
                             return text.split('-').join(' ');
                         }

在我看来,上面的条件应该用下面的代码代替

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

此条件检查所有即已定义的、不为空且不为空的字符串。

希望这可以帮助。

原文由 Nitin Garg 发布,翻译遵循 CC BY-SA 3.0 许可协议

我做了一个小的代码更改并为过滤器创建了一个单独的模块并在“appModule”中使用。

您必须在输入中使用“ng-model”。

这是工作脚本。

 <!DOCTYPE html>
      <html>
            <head>
                <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
       <script>
                     angular.module('dash',[]).filter('removeDashes',function(){
                         return function(text){
                            if(text!==undefined){
                                 return text.split('-').join(' ');
                             }
                         }
                     });
angular.module('appModule', ['dash']).controller('someCTRL', function($scope){
                     });
                </script>
             </head>

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

原文由 Siva 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题