angularJs使用$watch和$filter过滤器制作搜索筛选实例
泠泠在路上 人气:0整理文档,搜刮出一个angularJs使用$watch和$filter过滤器制作搜索筛选,稍微整理精简一下做下分享。
<div ng-app="module" ng-controller="ctrl"> 搜索: <input type="text" ng-model="search"> <table border="1" width="600"> <tr> <td>编号</td> <td>点击数</td> <td>标题</td> </tr> <tr ng-repeat="(k,v) in lists"> <td>{{v.id}}</td> <td>{{v.click}}</td> <td>{{v.title}}</td> </tr> </table> </div> <script> var m = angular.module('module', []); m.controller('ctrl', ['$scope', '$filter', function ($scope, $filter) { $scope.data = [ {id: 1, click: 100, title: '百度'}, {id: 2, click: 200, title: '腾讯'}, {id: 3, click: 300, title: '谷歌'}, ]; //临时数据用于显示 $scope.lists = $scope.data; $scope.$watch('search',function(n,o){ $scope.lists = $filter('filter')($scope.data,n); }); }]); </script>
效果图:
加载全部内容