angularJS 中 filter

自定义过滤器:

module.filter('过滤器名称',function(){

  return function ('带过滤的数据' , '条件1' , '条件2'){

    return '带过滤的数据' ? '条件1' : '条件2'

  }

})

<!DOCTYPE html>
<html ng-app="MyApp">
<head>
    <title>Your first directive</title>
    <script src="angular.js">
    </script>
    <script type="application/javascript">
        var module = angular.
            module('MyApp', []);
    </script>
</head>

<body>
    <div ng-controller="RequestsController">
        <div ng-repeat="request in requests">
            <a ng-href="{{ request.done | conditional:'/history':'/request' }}">
                {{ request.done | conditional:'Done':'In Progress' }}
            </a>
        </div>
    </div>

    <script type="text/javascript">
        function RequestsController($scope) {
            $scope.requests = [];
            for (var i = 0; i < 50; ++i) {
                $scope.requests.push({ done : (i % 3 == 0) });
            }
        }

        module.filter('conditional', function() {
            return function(b, t, f) {
                return b ? t : f;
            };
        });
    </script>
</body>

</html>

  

posted @ 2018-10-31 15:58  董七  阅读(120)  评论(0编辑  收藏  举报