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

首頁 > 網站 > WEB開發 > 正文

(8)AngularJS 1.X 之過濾器

2024-04-27 15:11:25
字體:
來源:轉載
供稿:網友

引言在表達式中使用過濾器1 currency過濾器的使用2 lowercase過濾器的使用3 uppercase過濾器的使用4 json過濾器的使用5 data過濾器的使用6 number過濾器的使用7 limitTo過濾器的使用8 orderBy過濾器的使用9 filter過濾器的使用過濾器在指令中的使用過濾器在服務中的使用js代碼中使用1 lowercase過濾器2 json過濾器3 uppercase過濾器4 limitTo過濾器5 date過濾器6 currency過濾器7 number過濾器8 orderBy過濾器9 filter過濾器10 過濾器在js對象使用總結

1.引言

      在本篇博客中主要介紹一下AngularJS過濾器,過濾器主要應用于表達式,部分指令,過濾器還可以應用在控制器中(過濾器服務),AngularJS一共給我們提供了9種過濾器,分別為:currency,date,filter,json,limitTo,lowercase,number,orderBy,uppercase,首先我們看一下這個每一種過濾器所起到的作用:

currency:格式化數字為貨幣形式date:格式化date對象到字符串filter:從array中選擇一個條目子集,并作為一個新數組返回json:允許轉換一個javaScript對象到JSON字符串limitTo:截取字符串lowercase :轉換字符串為小寫形式。number : 格式化數字到文本。orderBy:字符串按照字母排序uppercase:轉換字符串到大寫形式。

上述九種過濾器便是AngularJS提供的過濾器,接下來我們就來看看過濾器的各種用法。

2. 在表達式中使用過濾器

2.1 currency過濾器的使用

      currency過濾器主要用于貨幣的格式化,它的使用方式為:{{ currency_exPRession | currency : symbol}}

currency_expression:表示AngularJS表達式currency:代表使用 currency過濾器symbol:是一個可選參數,如果不寫,默認是美元符。

      我們看一下這個過濾器的使用,代碼如下:

如果不寫symbol參數 {{100|currency}} //輸出結果為:$100.00如果寫symbol參數(也就是說:symbol參數控制貨幣的符號){{100|currency:'&&&&'}} //輸出結果為:&&&&100.00{{100|currency:'¥'}} //輸出結果為:¥100.00

2.2 lowercase過濾器的使用

這個過濾器很簡單就是將字符串字母變為小寫字母{{ uppercase_expression | lowercase}},代碼如下://注意這里寫的是字符串{{'ABCD'|lowercase}}//輸出結果為:abcd

2.3 uppercase過濾器的使用

這個過濾器很簡單就是將字符串字母變為大寫字母{{ uppercase_expression | uppercase}},代碼如下:{{'abcd'|uppercase}}//輸出結果為:ABCD

2.4 json過濾器的使用

json過濾器也非常簡單,沒有任何參數,只是將json對象轉換成字符串輸出,該過濾器為:{{ json_expression | json}},代碼如下:{{ {foo: "bar", baz: 23} | json }}//輸出結果為:{ "foo": "bar", "baz": 23 }

2.5 data過濾器的使用

      data過濾器是格式化輸出我們的data對象,該過濾器為:{{ date_expression | date : format}}

date_expression:是表達式date:使用data過濾器format:可選參數,日期輸出的格式化

接下來我們使用一下這個過濾器

不使用format參數 //這是我們的控制器 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.time=new Date(); }); //這是我們的表達式 {{ time | date }}//輸出結果為:Feb 7, 2017使用format參數 //這是我們的控制器 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.time=new Date(); }); //這是我們的表達式 {{ time | date :"'yyyy-MM-dd"}}//輸出結果為:2017-02-07

2.6 number過濾器的使用

關于number過濾器的使用,是格式化數字輸出,這個過濾器為:{{ number_expression | number : fractionSize}}

number_expression:表達式number:使用number過濾器fractionSize:可選參數,可以控制數字的小數點位數,,如果不填,默認為3

現在我們看一下這個過濾器的使用。

沒有使用fractionSize參數{{ 1.234567 | number }}//輸出結果為:1.234使用fractionSize參數{{ 1.234567 | number:6 }}//輸出結果為:1.234567

2.7 limitTo過濾器的使用

limitTo過濾器是用來截取字符串和對對象,他的使用方式為:{{ limitTo_expression | limitTo : limit}}

limitTo_expression:字符串表達式或者對象limitTo:使用limitTo過濾器limit:代表字符串截取的長度,如果是正數是從前面截取,如果是負數是從后面截取

接下來我們看一下這個過濾器的使用

字符串的截取{{ 'abcde' | limitTo:2 }}//運行結果:ab{{ 'abcde' | limitTo:-2 }}//運行結果:de對象的截取 //創建我們的控制器 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.persons= [ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] }); //表達式 {{persons| limitTo:1}} //運行結果 [{"age":20,"id":10,"name":"iphone"}]

2.8 orderBy過濾器的使用

關于orderBy過濾器的使用:字符串按字母順序排序,數字按大小排序,這個表達式為:{{ orderBy_expression | orderBy : expression : reverse}}

orderBy_expression:排序的字符串orderBy:使用orderBy過濾器expression:一個決定順序的聲明reverse:是一個boollen類型,是正序排列還是逆序排列,如果不填默認為false

接下來我們用幾個例子來看一下這個過濾器的使用:

根據id正序排列 //定義我們的控制器 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.persons= [ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] }); //我們的表達式,根據id字段正序排序 {{persons| orderBy:'id':false}} //上面表達式等價于 {{persons| orderBy:'id'}} //運行結果 [{"age":20,"id":10,"name":"iphone"},{"age":12,"id":11,"name":"sunm xing"},{"age":44,"id":12,"name":"test abc"}]根據id逆序排列 //定義我們的控制器 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.persons= [ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] }); //我們的表達式,根據id字段正序排序 {{persons| orderBy:'id':true}} //運行結果 [{"age":44,"id":12,"name":"test abc"},{"age":12,"id":11,"name":"sunm xing"},{"age":20,"id":10,"name":"iphone"}]

2.9 filter過濾器的使用

filter過濾器主要用于查找,也就是在array中選擇一定的條目,然后返回一個新數組,該過濾器的形式為:{{ filter_expression | filter : expression : comparator}}

filter_expression:代表數組源filter:使用filter過濾器expression:篩選的條件comparator:是一個比較器,比如篩選的時候是否區分大小寫,這個可以是一個函數對象,用于比較。

現在我們來看一下這個過濾器的使用

代碼實現 //定義我們的控制器 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.persons= [ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] }); //表達式(查找帶有字符串s的行) {{persons|filter:'s'}} //運行結果 [{"age":12,"id":11,"name":"sunm xing"},{"age":44,"id":12,"name":"test abc"}] //表達式(注意:{'name':'iphone'}后面有一個空格)查找name字段為iphone的對象 {{ persons | filter:{'name':'iphone'} }} //運行結果 [{"age":20,"id":10,"name":"iphone"}]

3.過濾器在指令中的使用

關于過濾器在指令中的使用在這里舉一個例子ng-repeat指令中的使用,代碼如下:

//控制器的實現 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.persons= [ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] }); //html片段(根據name字段篩選對象) <input type="text" ng-model="my"> <div> <li ng-repeat="p in persons|filter:{name:my}">{{p.id}}---{{p.age}}---{{p.name}}</li> </div>運行結果

這里寫圖片描述

4. 過濾器在服務中的使用(js代碼中使用)

      在前面我們使用了過濾器,分別是在表達式中使用,還有就是在指令中使用,其實過濾器的使用還可以在js代碼中使用,AngularJS為我們提供了一個$filter服務,此服務可以注入到我們js代碼中使用,接下來我們就來簡單的看一下這9種過濾器在js代碼中如何使用呢?

4.1 lowercase過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('lowercase')("ABCD") //text值為abcd });

4.2 json過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('json')({name:"wpx",age:20}) }); //text值是字符串:{ "name": "wpx", "age": 20 }

4.3 uppercase過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('uppercase')("abcd") //text值為ABCD });

4.4 limitTo過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('limitTo')("abcde","2") }); //text值為ab var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('limitTo')("abcde","-2") }); //text值為de

4.5 date過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('date')(new Date(),"yyyy-MM-dd") }); //text返回值為:2017-02-07

4.6 currency過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('currency')(20,"%") }); //text的值為:%20.00

4.7 number過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('number')(20,"4") }); //text的值為:20.0000

4.8 orderBy過濾器

//逆序排列我們的數組,返回值text是一個對象數組 var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('orderBy')([ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ],"id",true) });

4.9 filter過濾器

var app=angular.module("myApp",[]); app.controller("firstController", function ($scope,$filter) { $scope.text=$filter('filter')([ {"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ],{name:"iphone"}) }); //返回值是一個對象,是filter過濾器篩選之后的結果

4.10 過濾器在js對象使用總結

過濾器在Javascript中的使用一般形式是這樣的:$filter()()

$filter服務有兩個參數,第一個參數是過濾器的名稱,第二個參數是一個參數列表。我們來看該服務的第二個參數,第二個參數的第一個是輸入的值,后面的參數分別為過濾器所需要的內容。以orderBy過濾器為例,看一下這個服務$filter('orderBy')(array, expression, reverse),我們看第二個參數array是輸入的排序數組,expression是過濾表達式,reverse代表是否逆序排列。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 讷河市| 喜德县| 武冈市| 宁武县| 融水| 大丰市| 石泉县| 温宿县| 六盘水市| 陆良县| 哈密市| 宜君县| 丰原市| 泰州市| 左贡县| 陕西省| 浮山县| 八宿县| 桑植县| 临朐县| 华池县| 韩城市| 奉贤区| 闽清县| 六枝特区| 商都县| 盘锦市| 叙永县| 伊金霍洛旗| 马尔康县| 泊头市| 西丰县| 闵行区| 阿拉尔市| 曲周县| 碌曲县| 晋州市| 莱州市| 卢氏县| 盐城市| 铁岭市|