Angularjs的文字过滤,*符号转换

来源:互联网 发布:笔记本电脑拍照软件 编辑:程序博客网 时间:2024/05/21 08:52


<!DOCTYPE html>
<html>

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

      //需要导入/angular.js库文件

        <script type="text/javascript" src="../angular-1.5.5/angular-1.5.5/angular.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>