HTML angular追加内容,并进行判断原先是否存在和是否存在敏感字符

来源:互联网 发布:印度历史知乎 编辑:程序博客网 时间:2024/06/07 02:57
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="angular.js"></script>
        <script>
            var haha = 3;
            var app = angular.module("myApp", []);
            //自定义过滤器
            app.filter("myFilter", function() {
                return function(text) {
                    //alert("fasd");
                    if(text.indexOf("敏感字符") >= 0) {
                        //alert("asdfasdf");
                        alert("包含敏感字符")
                        return text.replace(/敏感字符/g, "***");
                    }
                    return text;
                }
            });
            app.controller("myCtrl", function($scope) {
                $scope.newRecord = "";
                $scope.selectRecord = "";
                $scope.records = ["中午花了20块钱吃早饭", "早上花了5块钱吃早饭"];
                $scope.addRecord = function() {
                    if($scope.newRecord == "" || $scope.newRecord == null) {
                        alert("输入内容为空");
                    } else {
                        $scope.records.unshift($scope.newRecord);

                        //alert($scope.records[2]);
                    }
                };
                var flag = true;
                $scope.selectRecordMethod = function() {
                    for(record in $scope.records) {
                        if($scope.records[record] == $scope.selectRecord) {
                            alert("已经存在");
                            flag = false;
                        } else if($scope.selectRecord == "敏感字符") {
                            alert("搜索内容存在敏感字符");
                            flag = false;
                            break;
                        }
                    }
                    if(flag) {
                        alert("不存在");
                    }
                }
                /*$scope.myFilter = function(text){
                    var reg = /敏感字符/;
                    if(reg.test(text)){
                        return text.replace(/敏感字符/g,"***");
                    }
                }*/
            });
        </script>
    </head>

    <body ng-app="myApp" ng-controller="myCtrl">
        <center>
            记事本
            <div style="width: 300px; height: 250px; border: 1 solid blue; background-color:#A6E1EC;">
                <p ng-repeat="record in records track by $index">{{record | myFilter }}</p>
            </div><br/> 输入框:
            <input type="text" ng-model="newRecord" /><br/>
            <button ng-click="addRecord()">记录</button><br/> 搜索:
            <input type="text" ng-model="selectRecord" /><br/>
            <button ng-click="selectRecordMethod()">记录</button>
        </center>
    </body>

</html>
原创粉丝点击