angularjs filter 詳解

系統的學習了一下angularjs,發現angularjs的有些思想根php的模塊smarty很像,例如數據綁定,filter。若是對smarty比較熟悉的話,學習angularjs會比較容易一點。這篇簡單說一下angularjs的filter功能,angularjs的filter功能可分爲二種,一種是內置的過濾器,一種是自定義的。php

 

一,內置的過濾器html

1,uppercase,lowercase大小轉換jquery

  1. {{ "lower cap string" | uppercase }}     //結果:LOWER CAP STRING  
  2. {{ "TANK is GOOD" | lowercase }}         //結果:tank is good  

|這裏的豎線是一種管道功能,若是對linux比較熟悉的話,這塊的|根linux的管道功能,基本是同樣的linux

2,json格式化angularjs

  1. {{ {foo: "bar", baz: 23} | json }}    //結果:{ "foo": "bar", "baz": 23 }  

注意:bza沒格式前是沒有雙引號的,格式化後就轉換成了json數據了。json

3,date格式化mvc

  1. {{ 1304375948024 | date }}                          //結果:May 3, 2011  
  2. {{ 1304375948024 | date:"MM/dd/yyyy @ h:mma" }}     //結果:05/03/2011 @ 6:39AM  
  3. {{ 1304375948024 | date:"yyyy-MM-dd hh:mm:ss" }}    //結果:2011-05-03 06:39:08  

4,number格式化app

  1. {{ 1.234567 | number:1 }}    //結果:1.2  
  2. {{ 1234567 | number }}       //結果:1,234,567  

5,currency貨幣格式化框架

  1. {{ 250 | currency }}                 //結果:$250.00  
  2. {{ 250 | currency:"RMB ¥ " }}       //結果:RMB ¥ 250.00  

6,filter查找iphone

  1. {{ [{"age": 20,"id": 10,"name": "iphone"},  
  2. {"age": 12,"id": 11,"name": "sunm xing"},  
  3. {"age": 44,"id": 12,"name": "test abc"}  
  4. ] | filter:'s'}}    //查找含有有s的行  
  5.   
  6. //上例結果:[{"age":12,"id":11,"name":"sunm xing"},{"age":44,"id":12,"name":"test abc"}]  
  7.   
  8. {{ [{"age": 20,"id": 10,"name": "iphone"},  
  9. {"age": 12,"id": 11,"name": "sunm xing"},  
  10. {"age": 44,"id": 12,"name": "test abc"}  
  11. ] | filter:{'name':'iphone'} }}   //查找name爲iphone的行  
  12.   
  13. //上例結果:[{"age":20,"id":10,"name":"iphone"}]  

7,limitTo字符串,對像的截取

  1. {{ "i love tank" | limitTo:6 }}           //結果:i love  
  2. {{ "i love tank" | limitTo:-4 }}          //結果:tank  
  3.   
  4. {{ [{"age": 20,"id": 10,"name": "iphone"},  
  5. {"age": 12,"id": 11,"name": "sunm xing"},  
  6. {"age": 44,"id": 12,"name": "test abc"}  
  7. ] | limitTo:1 }}     //結果:[{"age":20,"id":10,"name":"iphone"}]  

8,orderBy對像排序

  1. {{ [{"age": 20,"id": 10,"name": "iphone"},  
  2. {"age": 12,"id": 11,"name": "sunm xing"},  
  3. {"age": 44,"id": 12,"name": "test abc"}  
  4. ] | orderBy:'id':true }}        //根id降序排  
  5.   
  6. {{ [{"age": 20,"id": 10,"name": "iphone"},  
  7. {"age": 12,"id": 11,"name": "sunm xing"},  
  8. {"age": 44,"id": 12,"name": "test abc"}  
  9. ] | orderBy:'id' }}           //根據id升序排  

二,自定filter功能

我找了一個基本angularjs的mvc框架,phonecat,自定義filter也是在這基礎寫的,這個框架挺好用的。

1,filters.js添加一個module

  1. angular.module('tanktest', []).filter('tankreplace', function() {  
  2.     return function(input) {  
  3.         return input.replace(/tank/, "=====")  
  4.     };  
  5. });  

2,app.js中加載這個module

  1. var phonecatApp = angular.module('phonecatApp', [  
  2.   'ngRoute',  
  3.   'phonecatControllers',  
  4.   'facebookControllers',  
  5.   'tanktest'  
  6. ]);  

3,html中調用

  1. {{ "TANK is GOOD" | lowercase |tankreplace}}   //結果:===== is good  
相關文章
相關標籤/搜索