angularjs自定义过滤器

angularjs中的自定一过滤器很有必要掌握,我觉得相当于后端语言中的模板函数概念,在模板中定义是这样的<div>{{user | myfilter:user}}</div>,然后myfilter就是我们定义的一个模板函数,他专门处理某种计算,下面看看他的demo

<!DOCTYPE html>
<html>
<head>
	<title>angularJS demo</title>
	<meta charset="utf-8">		
</head>
<body>

	<form ng-app="myapp" name="myForm" ng-controller="test">
		<div>{{user | myfilter:user}}</div>
	</form>

	<script src="//www.w3cschool.cc/try/angularjs/1.2.5/angular.min.js"></script>
	<script type="text/javascript">
	function test($scope){
		$scope.user = "张三";
		$scope.email = "[email protected]";
	}

	var app = angular.module("myapp",[]).controller("test",test).
		filter("myfilter",function() {
			return function(obj,args){
				console.log(obj,args);
				return "处理后的对象";
			}
	});
	</script>
	
</body>	
</html>




你可能感兴趣的:(angularjs自定义过滤器)