angularJs实现文字过滤

<!DOCTYPE html>
<html>




    <head>
        <meta charset="UTF-8">
        <title>实现文字过滤,转换**符号</title>




    <!--
        需要导入/angular.js库文件
        -->




       <script type="text/javascript" src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
        <script type="text/javascript">
            var app = angular.module("myApp", []);
            app.filter("replace", function() {
                return function(text) {
                    //alert(text.indexOf("法轮功"));
                    //return text+text.contains("法轮功");




                    function replaceValue(text) {
                        var value = "";
                        if(text.indexOf("法轮功")>=0) {
                            //alert("111");
                            value =  text.replace(/法轮功/g, "***");
                            
                            if(value.indexOf("枪")>=0){
                                return value.replace(/枪/g, "*");
                            }else{
                                return value;
                            }
                        }else{
                            if(value.indexOf("枪")>=0){
                                return text.replace(/枪/g, "*");
                            }else{
                                return text;
                            }
                        }
                        
                    }
                    return replaceValue(text);




                    //return text.replace(/法轮功|枪    /g, "***");




                }
            });
            app.controller("myCtrl", function($scope) {
                $scope.value = "法轮功";
            })
        </script>
    </head>




    <body ng-app="myApp" ng-controller="myCtrl">
        请输入:<input ng-model="value" />
        <p>{{value | replace}}</p>
    </body>




</html>

猜你喜欢

转载自blog.csdn.net/lucky_7777777/article/details/79419807