关于angular $filter,自定义函数过滤和自定义过滤器

2017-04-21  本文已影响0人  晚上吃火锅吗

关于$filter的自定义过滤器。要是定义在moduler内部使用自定义函数的方式,那么传入该函数的是object。要是定义成filter的话,则传入filter的则是array。

<div ng-app="myApp" ng-controller="namesCtrl">
  <ul>
    <li ng-repeat="x in names | filter:myFilter1"> 
    <!-- 传入object -->
      {{x}}
    </li>
  </ul> 
 <ul>
    <li ng-repeat="x in names | myFilter2">
    <!-- 传入array -->
      {{x}}
    </li>
  </ul>
</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope,$filter) {
    $scope.index = 2;
    $scope.size = 3;
    $scope.names = [
        {name:'11'},
        {name:'22'},
        {name:'33'},
        {name:'44'},
        {name:'55'},
        {name:'66'},
        {name:'77'}
    ];

    $scope.myFilter1 = function(data){
      console.log(data);
      }
    }
}).filter('myFilter2',function($filter){
  return function(data){
    console.log(data);   
  }
});

angular.module('demo',[])
</script>
上一篇下一篇

猜你喜欢

热点阅读