AngularJS實現存入文本,搜索文本,敏感字符

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>實現存入,搜索,敏感字符</title>
    <style type="text/css">
        .text01{
            width: 500px;
            height:300px;
            border: 1px solid red;
            background-color: #ece5d5;
        }
    </style>
    <script src="angular.js"></script>
    <script type="text/javascript">
        var app = angular.module("myApp",[]);
        //自定義過濾器,過濾敏感字符
        app.filter("myFilter",function () {
            return function (text) {
                if(text.indexOf("敏感字符")>=0){
                    alert("有敏感字符");
                    //有敏感字符需要過濾
                    return text.replace(/敏感字符/g,"****");
                }
                //將所有文本返回
                return text;
            }
        })
        app.controller("myCtrl",function ($scope) {
            $scope.arr = ["今天早上喫早餐花了10元","今天中午喫午飯花了20元"];
            $scope.newarr = "";
            $scope.addarr = function () {
                //非空判斷,爲空時不添加
                if($scope.newarr == null || $scope.newarr == ""){
                    alert("內容不可爲空");
                }else{
                    $scope.arr.unshift($scope.newarr);
                }
            }
            //搜索文本
            var flag = true;
            $scope.sousuo = function () {
                if($scope.newsousuo == null || $scope.newsousuo == ""){
                    alert("不可爲空");
                }else{
                    for(a in $scope.arr){
                        if($scope.arr[a] == $scope.newsousuo){
                            alert("已存在");
                            flag = false;
                        }
                    }
                    if (flag) {
                        alert("不存在");
                    }
                }

            }
        });
    </script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <p>記賬本:</p>
    <div class="text01">
        <p  ng-repeat="i in arr">{{i | myFilter}}</p>
    </div>
    輸入框:<input type="text" ng-model="newarr"><button ng-click="addarr()">記錄</button><br>
    搜索框:<input type="text" ng-model="newsousuo"><button ng-click="sousuo()">搜索</button>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章