码迷,mamicode.com
首页 > Web开发 > 详细

angularJs的过滤器扩展及自定义过滤器

时间:2015-06-10 06:31:34      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

一、过滤器扩展

  1、过滤器的组合使用

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script>
    var m1 = angular.module(myApp,[]);    
    m1.controller(Aaa,[$scope,function($scope){
        $scope.name = hello;
    }]);
</script>
</head>
<body>
<div ng-controller="Aaa">
    <p>{{ name | limitTo : 2 | uppercase }}</p>       
</div>
</body>
</html>

 

  2、通过服务的方式在js中使用过滤器

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script>
    var m1 = angular.module(myApp,[]);    
    m1.controller(Aaa,[$scope,$filter,function($scope,$filter){
        //$scope.name = $filter(‘uppercase‘)(‘hello‘);   //对hello进行转大写的操作
        
        //$scope.name = $filter(‘number‘)(‘1326554654‘);
        $scope.name = $filter(number)(1326554654.545485,4);  //4表示设置保留的小数位
    }]);
</script>
</head>
<body>
<div ng-controller="Aaa">
    <p>{{ name }}</p>
</div>
</body>
</html>

 

 

 

 

二、自定义过滤器

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script>
    var m1 = angular.module(myApp,[]);
    
    //自定义过滤器
    m1.filter(firstUpper,function(){
        return function(str,num){
            //console.log(num);      //num没有什么用,但是可以找到这个num
            return str.charAt(0).toUpperCase() + str.substring(1);
        }
    });
        
    m1.controller(Aaa,[$scope,$filter,function($scope,$filter){
        $scope.name = $filter(firstUpper)(hello);
    }]);
</script>
</head>
<body>
<div ng-controller="Aaa">
    <p>{{ name }}</p>
    <p>{{ name | firstUpper }}</p>
</div>
</body>
</html>

 

angularJs的过滤器扩展及自定义过滤器

标签:

原文地址:http://www.cnblogs.com/LO-ME/p/4565042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!