//第一件事还是导包
<body ng-app="myApp" ng-controller="myCtrl">
//定义一个输入框
<input type="text" ng-model="data" />
<div id="one">{{data|mc}}</div>
<script>
var mo = angular.module("myApp",[]);
mo.controller("myCtrl",function($scope){
});
//自定义过滤器
mo.filter("mc",function(){
return function(input){
//这个一定要加不然会报错
if(input==undefined){
input="";
}
//遍历全局有“哈”字就替换成“fuck”
return input.replace(/哈/g,"fuck");
}
});
</script>
</body>
//一个简单的小案例
//建立输入框输入数据来查询数组内有没有有就展示
<body ng-app="myApp" ng-controller="myCtrl">
用户名:<input type="text" ng-model="name"/><br/>
密码:<input type="text" ng-model="pwd" />
<span>{{pwd.length>6?"密码过长":""}}</span><br />
{{names|mohu:name}}
<script type="text/javascript">
var mo = angular.module("myApp",[]);
mo.controller("myCtrl",function($scope){
$scope.names = ["zz","lxy","wmy","atm","atm1","atm2"];
});
mo.filter("mohu",function(){
return function(input,n){
var newArr = [];
for(var i=0;i<input.length;i++){
var name = input[i];
if(name.indexOf(n)!=-1){
newArr.push(name);
}
}
return newArr;
}
})
</script>
</body>