国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用AngularJS創(chuàng)建自定義的過濾器的方法

2019-11-20 12:13:06
字體:
供稿:網(wǎng)友

Angularjs過濾器是 angularjs非常棒的特性之一。有朝一日,你可能需要使用自定義過濾器,幸運的是,你找到了這篇博文。

下面顯示的是自定義過濾器長什么樣子(請注意myfilter):

我們的自定義過濾器叫做 "myfilter", 它有由 ':'隔開的4個參數(shù).

這是一個將會用到的樣本輸入:


  $scope.friends = [{name:'John', phone:'555-1276'},            {name:'Annie', phone:'800-BIG-MARY'},            {name:'Mike', phone:'555-4321'},            {name:'Adam', phone:'555-5678'},            {name:'David', phone:'555-8765'},            {name:'Mikay', phone:'555-5678'}];

過濾器只顯示電話號碼中含有 "555"的項,這是樣本輸出:

 

 Name   Phone  John   555-1276  Mike   555-4321  Adam   555-5678  David   555-8765  Mikay   555-5678

過濾"555"的處理流程由 "windowScopedFilter"執(zhí)行, 它是過濾器 'myfilter'的第四個參數(shù).
 

下面我們來實現(xiàn)這些功能 (把logging添加到每個輸入?yún)?shù)):
 

 var myapp = angular.module('MyFilterApp', []);  myapp.filter('myfilter', function() {   return function(input, param1) {    console.log("------------------------------------------------- begin dump of custom parameters");    console.log("input=",input);    console.log("param1(string)=", param1);    var args = Array.prototype.slice.call(arguments);    console.log("arguments=", args.length);    if (3<=args.length) {       console.log("param2(string)=", args[2]);    }    if (4<=args.length) {       console.log("param3(bool)=", args[3]);    }    console.log("------------------------------------------------- end dump of custom parameters");    // filter    if (5<=args.length) {       return window[args[4]](input);    }    return input;   };  });

上面的代碼大多都log了(譯者注:將信息顯示到控制臺). 實際完成過濾的最重要的一部分是:
 

   // filter    if (5<=args.length) {       return window[args[4]](input);    }    return input;


"return window[args[4]](input)" 調(diào)用第四個參數(shù), 它是 'windowScopedFilter'.

這是控制臺輸出:

  "------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21  "input=" [object Array] custom_filter_function.html:22  "param1(string)=" "param1" custom_filter_function.html:23  "arguments=" 5 custom_filter_function.html:25  "param2(string)=" "param2" custom_filter_function.html:27  "param3(bool)=" true custom_filter_function.html:30  "------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32  "------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21  "input=" [object Array] custom_filter_function.html:22  "param1(string)=" "param1" custom_filter_function.html:23  "arguments=" 5 custom_filter_function.html:25  "param2(string)=" "param2" custom_filter_function.html:27  "param3(bool)=" true custom_filter_function.html:30  "------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32

完整代碼:
 
 

<html>  <head>  <script src="angular.min.js"></script>  <script type="text/javascript">  function windowScopedFilter (input) {    var output = [];    angular.forEach(input, function(v,k){       if (v.phone.contains("555")) {         output.push(v);       }    });    return output;     }  var myapp = angular.module('MyFilterApp', []);  myapp.filter('myfilter', function() {   return function(input, param1) {    console.log("------------------------------------------------- begin dump of custom parameters");    console.log("input=",input);    console.log("param1(string)=", param1);    var args = Array.prototype.slice.call(arguments);    console.log("arguments=", args.length);    if (3<=args.length) {       console.log("param2(string)=", args[2]);    }    if (4<=args.length) {       console.log("param3(bool)=", args[3]);    }    console.log("------------------------------------------------- end dump of custom parameters");    // filter    if (5<=args.length) {       return window[args[4]](input);    }    return input;   };  });  myapp.controller('MyFilterController', ['$scope', function($scope) {   $scope.friends = [{name:'John', phone:'555-1276'},            {name:'Annie', phone:'800-BIG-MARY'},            {name:'Mike', phone:'555-4321'},            {name:'Adam', phone:'555-5678'},            {name:'David', phone:'555-8765'},            {name:'Mikay', phone:'555-5678'}];  }]);  </script>  </head>  <body ng-app="MyFilterApp">  <div ng-controller="MyFilterController">    <table id="searchTextResults">     <tr><th>Name</th><th>Phone</th></tr>     <tr ng-repeat="friend in friends |myfilter:'param1':'param2':true:'windowScopedFilter'">     <td>{{friend.name}}</td>     <td>{{friend.phone}}</td>     </tr>    </table>  </div>  <hr>  </body>  </html>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 特克斯县| 永春县| 大英县| 金阳县| 太保市| 两当县| 潮州市| 三河市| 锡林浩特市| 屯留县| 江达县| 阜平县| 融水| 昭通市| 防城港市| 博野县| 西林县| 渝北区| 西吉县| 山阳县| 常德市| 昌吉市| 安国市| 武夷山市| 承德县| 巴中市| 彰化市| 双柏县| 郎溪县| 安阳县| 广饶县| 安溪县| 辽宁省| 潢川县| 大足县| 汶上县| 北海市| 土默特左旗| 凤山县| 靖远县| 阿尔山市|