search

Home  >  Q&A  >  body text

angular.js - How to filter special characters in textarea in angularjs?

How does angularjs filter out special characters contained in textarea input content?

PHPzPHPz2776 days ago662

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-05-15 16:53:44

    Define a filter yourself

    For example:

    html<body ng-app="app">
        <p ng-controller="main">
            <textarea ng-model="val" ng-change="cg()"></textarea>
            <p>
                原始值:<pre>{{val}}</pre>
                在HTML中使用过滤器显示的值:<pre>{{val | replaceCode}}</pre>
                过滤后的值:<pre>{{replacedVal}}</pre>
            </p>
        </p>
        <script type="text/javascript">
        var app = angular.module('app', []);
    
        app.controller('main', ['$scope', 'replaceCodeFilter', function($scope, replaceCodeFilter){
            // 注意这里 可以等到你真正的需要取值的时候再去调用就可以了
            // 这里只是为了演示 所以加了 on-change
            $scope.cg = function() {
                $scope.replacedVal = replaceCodeFilter($scope.val);
            };
    
        }]);
    
        app.filter('replaceCode', function() {
            return function(val) {
                if (!val) val = '';
                return val.replace(/[#$%^&*!]/g, '');
            }
        });
        </script>
    </body>
    

    reply
    0
  • Cancelreply