最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年06月18日 15:12:20   投稿:goldensun  
這篇文章主要介紹了使用AngularJS創(chuàng)建自定義的過濾器的方法,AngularJS是非常熱門的JavaScript庫,需要的朋友可以參考下

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>

相關(guān)文章

  • 詳解如何在Angular中使用環(huán)境變量

    詳解如何在Angular中使用環(huán)境變量

    如果你正在構(gòu)建一個使用 API 的應(yīng)用程序,你會想在開發(fā)過程中使用測試環(huán)境的 API 密鑰,而在生產(chǎn)環(huán)境中使用生產(chǎn)環(huán)境的 API 密鑰,在本教程中,你將學習如何在 Angular 中使用環(huán)境變量,需要的朋友可以參考下
    2024-02-02
  • AngularJS下對數(shù)組的對比分析

    AngularJS下對數(shù)組的對比分析

    下面小編就為大家?guī)硪黄狝ngularJS下對數(shù)組的對比分析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • 淺談angularJs函數(shù)的使用方法(大小寫轉(zhuǎn)換,拷貝,擴充對象)

    淺談angularJs函數(shù)的使用方法(大小寫轉(zhuǎn)換,拷貝,擴充對象)

    今天小編就為大家分享一篇淺談angularJs函數(shù)的使用方法(大小寫轉(zhuǎn)換,拷貝,擴充對象),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 對比分析AngularJS中的$http.post與jQuery.post的區(qū)別

    對比分析AngularJS中的$http.post與jQuery.post的區(qū)別

    這篇文章主要給大家對比分析AngularJS中的$http.post與jQuery.post的區(qū)別,十分的詳細,是篇非常不錯的文章,這里推薦給小伙伴們。
    2015-02-02
  • AngularJS教程之MVC體系結(jié)構(gòu)詳解

    AngularJS教程之MVC體系結(jié)構(gòu)詳解

    本文主要講解AngularJS MVC體系結(jié)構(gòu),這里提供詳細的教程供大家學習參考,有需要的小伙伴可以參考下
    2016-08-08
  • Angularjs單選改為多選的開發(fā)過程及問題解析

    Angularjs單選改為多選的開發(fā)過程及問題解析

    在項目中遇到這樣的需求想把下拉框的單選改為多選,怎么實現(xiàn)呢?下面小編通過本文給大家分享angularjs單選改為多選的開發(fā)過程及問題解析,需要的朋友參考下
    2017-02-02
  • AngularJS service之select下拉菜單效果

    AngularJS service之select下拉菜單效果

    這篇文章主要為大家詳細介紹了AngularJS service之select下拉菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • AngularJS入門教程引導程序

    AngularJS入門教程引導程序

    本文主要介紹AngularJS 引導程序,這里整理了相關(guān)資料及示例代碼,詳細講解了引導程序的知識要點,有興趣的小伙伴可以參考下
    2016-08-08
  • 解決Angular.Js與Django標簽沖突的方案

    解決Angular.Js與Django標簽沖突的方案

    AngularJS和django的模板都是用{{}}來引用變量的,這就導致了沖突,所以這篇文章主要就給大家介紹了如何解決Angular.Js與Django標簽沖突的方案,有需要的朋友們可以參考借鑒,下面來一起學習學習吧。
    2016-12-12
  • 測試IE瀏覽器對JavaScript的AngularJS的兼容性

    測試IE瀏覽器對JavaScript的AngularJS的兼容性

    這篇文章主要介紹了測試IE瀏覽器對JavaScript的AngularJS的兼容性的方法,盡管隨著Windows10的近期上市,IE瀏覽器即將成為歷史...需要的朋友可以參考下
    2015-06-06

最新評論

石林| 铅山县| 华宁县| 广平县| 都江堰市| 宁阳县| 育儿| 阆中市| 西吉县| 台州市| 盖州市| 乡城县| 靖远县| 长丰县| 景德镇市| 安乡县| 磴口县| 霍林郭勒市| 巴中市| 体育| 承德市| 临夏市| 静宁县| 开远市| 冕宁县| 阜阳市| 尼勒克县| 达日县| 大城县| 什邡市| 阿拉善右旗| 汾阳市| 和政县| 丰城市| 长泰县| 凤阳县| 延吉市| 余姚市| 伊春市| 哈密市| 伽师县|