<body ng-app="myapp" ng-controller="myCtrl"> <input type="text" ng-model="keytext"> <p>{{ keytext|wordFilter:"#" }}</p> </body>
//自定义过滤器,并且替换关键字
<script> var myapp=angular.module("myapp",[]); /*自定义过滤器*/ myapp.filter("wordFilter",function () { return function (msg,flag) { /*替换关键字*/ return msg.replace(/枪|子弹/g,flag); } }) myapp.controller("myCtrl",function ($scope) { $scope.keytext=""; }) </script>
效果如下: