angularJs使用$watch和$filter过滤器制作搜索筛选实例

Kelli ·
更新时间:2024-09-21
· 715 次阅读

整理文档,搜刮出一个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>

效果图:



filter AngularJS watch filter过滤器

需要 登录 后方可回复, 如果你还没有账号请 注册新账号