您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在angularjs中自定義過濾器demo,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
以下為數據:
$scope['classes']=[{id:1,name:"H503",capacity:40,software:"photoshop,金蝶,AI",freeTime:"周一1-2-3,周五1-2-3,周一5-6,"}, {id:2,name:"H504",capacity:50,software:"chrome,金蝶,office," ,freeTime:"周二1-2-3,周三3-4,周一5-6,"}, {id:3,name:"H505",capacity:60,software:"chrome,LOL,office," ,freeTime:"周二1-2-3,周三3-4,周四7-8,"} ];
具體功能:
分別在兩個select選擇星期和課次,列表會即時根據條件更新。
完整代碼:
<!DOCTYPE html> <html ng-app="myApp" > <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-controller="myCtrl"> <select ng-model="day"> <option selected="">周一</option> <option>周二</option> <option>周三</option> <option>周四</option> <option>周五</option> </select> <select ng-model="order"> <option selected="">1-2</option> <option>1-2-3</option> <option>3-4</option> <option>1-2-3-4</option> <option>5-6</option> <option>7-8</option> </select> <ul> <li ng-repeat="class in classes | myFiter:day:order"> <span>{{class.id}}</span> <span>{{class.name}}</span> <span>{{class.capacity}}</span> <span>{{class.software}}</span> </li> </ul> </select> </div> <script type="text/javascript"> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { //假數據 $scope['classes']=[{id:1,name:"H503",capacity:40,software:"photoshop,金蝶,AI",freeTime:"周一1-2-3,周五1-2-3,周一5-6,"}, {id:2,name:"H504",capacity:50,software:"chrome,金蝶,office," ,freeTime:"周二1-2-3,周三3-4,周一5-6,"}, {id:3,name:"H505",capacity:60,software:"chrome,LOL,office," ,freeTime:"周二1-2-3,周三3-4,周四7-8,"} ]; //select的默認值 $scope.order="1-2"; $scope.day="周一"; }); //自定義過濾器 app.filter('myFiter',function() { return function(input,day,order) { var time=day+order; var output=[]; for(var i=0;i<input.length;i++) { var n=input[i].freeTime.indexOf(time); // console.log(n); // console.log(input[i].freeTime.charAt(n+time.length)); if(n!=-1)//如果能找到 { if(input[i].freeTime.charAt(n+time.length)==',') //這樣做是為了防止1-2與1-2-3是一樣的結果 output.push(input[i]); } } return output; } }) </script> </body> </html>
運行效果:
看完上述內容,你們對怎么在angularjs中自定義過濾器demo有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。