詳解Angularjs filter過(guò)濾器
系統(tǒng)的學(xué)習(xí)了一下angularjs,發(fā)現(xiàn)angularjs的有些思想根php的模塊smarty很像,例如數(shù)據(jù)綁定,filter。如果對(duì)smarty比較熟悉的話,學(xué)習(xí)angularjs會(huì)比較容易一點(diǎn)。這篇簡(jiǎn)單說(shuō)一下angularjs的filter功能,angularjs的filter功能可分為二種,一種是內(nèi)置的過(guò)濾器,一種是自定義的。
Filter是用來(lái)格式化數(shù)據(jù)用的。
Filter的基本原型( '' 類似于Linux中的管道模式):
{{ expression filter }}
Filter可以被鏈?zhǔn)绞褂茫催B續(xù)使用多個(gè)filter):
{{ expression filter1 filter2 ... }}
Filter也可以指定多個(gè)參數(shù):
{{ expression filter:argument1:argument2:... }}
一,內(nèi)置的過(guò)濾器
1,uppercase,lowercase大小轉(zhuǎn)換
{{ "lower cap string" | uppercase }} //結(jié)果:LOWER CAP STRING {{ "TANK is GOOD" | lowercase }} //結(jié)果:tank is good
|這里的豎線是一種管道功能,如果對(duì)linux比較熟悉的話,這塊的|根linux的管道功能,基本是一樣的
2,json格式化
{{ {foo: "bar", baz: 23} | json }} //結(jié)果:{ "foo": "bar", "baz": 23 }
注意:bza沒(méi)格式前是沒(méi)有雙引號(hào)的,格式化后就轉(zhuǎn)換成了json數(shù)據(jù)了。
3,date格式化
{{ 1304375948024 | date }} //結(jié)果:May 3, 2011 {{ 1304375948024 | date:"MM/dd/yyyy @ h:mma" }} //結(jié)果:05/03/2011 @ 6:39AM {{ 1304375948024 | date:"yyyy-MM-dd hh:mm:ss" }} //結(jié)果:2011-05-03 06:39:08
4,number格式化
{{ 1.234567 | number:1 }} //結(jié)果:1.2 {{ 1234567 | number }} //結(jié)果:1,234,567
5,currency貨幣格式化
{{ 250 | currency }} //結(jié)果:$250.00 {{ 250 | currency:"RMB ¥ " }} //結(jié)果:RMB ¥ 250.00
6,filter查找
{{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | filter:'s'}} //查找含有有s的行 //上例結(jié)果:[{"age":12,"id":11,"name":"sunm xing"},{"age":44,"id":12,"name":"test abc"}] {{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | filter:{'name':'iphone'} }} //查找name為iphone的行 //上例結(jié)果:[{"age":20,"id":10,"name":"iphone"}]
7,limitTo字符串,對(duì)像的截取
{{ "i love tank" | limitTo:6 }} //結(jié)果:i love {{ "i love tank" | limitTo:-4 }} //結(jié)果:tank {{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | limitTo:1 }} //結(jié)果:[{"age":20,"id":10,"name":"iphone"}]
8,orderBy對(duì)像排序
{{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | orderBy:'id':true }} //根id降序排 {{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | orderBy:'id' }} //根據(jù)id升序排
二,自定filter功能
我找了一個(gè)基本angularjs的mvc框架,phonecat,自定義filter也是在這基礎(chǔ)寫的,這個(gè)框架挺好用的。
1,filters.js添加一個(gè)module
angular.module('tanktest', []).filter('tankreplace', function() { return function(input) { return input.replace(/tank/, "=====") }; });
2,app.js中加載這個(gè)module
var phonecatApp = angular.module('phonecatApp', [ 'ngRoute', 'phonecatControllers', 'facebookControllers', 'tanktest' ]);
3,html中調(diào)用
{{ "TANK is GOOD" | lowercase |tankreplace}} //結(jié)果:===== is good
注意:| lowercase |tankreplace管道命令可以有多個(gè)
以上所述是小編給大家介紹的angularjs filter過(guò)濾器相關(guān)知識(shí),希望對(duì)大家有所幫助,更多有關(guān)angularjs filter相關(guān)知識(shí)敬請(qǐng)關(guān)注腳本之家網(wǎng)站。謝謝!
相關(guān)文章
AngularJS全局警告框?qū)崿F(xiàn)方法示例
這篇文章主要介紹了AngularJS全局警告框?qū)崿F(xiàn)方法,結(jié)合實(shí)例形式分析了AngularJS全局警告框的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05詳解webpack+angular2開(kāi)發(fā)環(huán)境搭建
這篇文章主要介紹了詳解webpack+angular2開(kāi)發(fā)環(huán)境搭建,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06angular實(shí)現(xiàn)圖片懶加載實(shí)例代碼
本篇文章主要介紹了angular實(shí)現(xiàn)圖片懶加載實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06對(duì)angularJs中2種自定義服務(wù)的實(shí)例講解
今天小編就為大家分享一篇對(duì)angularJs中2種自定義服務(wù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Angular2學(xué)習(xí)教程之ng中變更檢測(cè)問(wèn)題詳解
這篇文章主要給大家介紹了Angular2學(xué)習(xí)教程之ng中變更檢測(cè)問(wèn)題的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05AngularJs html compiler詳解及示例代碼
本文主要介紹AngularJs html compiler的知識(shí)講解,這里整理了相關(guān)資料及相關(guān)示例代碼,有興趣的小伙伴可以參考下2016-09-09Angular實(shí)現(xiàn)類似博客評(píng)論的遞歸顯示及獲取回復(fù)評(píng)論的數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于Angular如何實(shí)現(xiàn)類似博客評(píng)論的遞歸顯示及獲取回復(fù)評(píng)論的數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11