filter、orderBy、json

例子:

index.html

<div ng-app="myApp">
    <div ng-controller="firstController">
        <p>
            <!-- [{"name":"上海11212","py":"shanghai"}]-->
            {{ data.city | filter : '上海'}}
        </p>
        <p>
            <!-- []-->
            {{ data.city | filter : 'name'}}
        </p>

        <p>
            <!-- name":"上海11212","py":"shanghai"},{"name":"北京","py":"beijing"}-->
            {{ data.city | filter : {py:'g'} }}
        </p>

        <p>
            <!-- name":"上海11212","py":"shanghai"},{"name":"北京","py":"beijing"}-->
            {{ data.city | filter : checkName }}
        </p>

        <p>
            <!-- [{"name":"北京","py":"beijing"},{"name":"上海11212","py":"shanghai"},{"name":"四川","py":"sichuan"}] -->
            <!-- 默认顺序是 正序 asc a~z -->
            {{ data.city | orderBy : 'py'}}

            <!-- 默认顺序是 反序 desc z~a  -->
            <!-- [{"name":"四川","py":"sichuan"},{"name":"上海11212","py":"shanghai"},{"name":"北京","py":"beijing"}] -->
            {{ data.city | orderBy : '-py'}}
        </p>
</div>
<script src="//cdn.bootcss.com/angular.js/1.4.8/angular.js"></script>
<script type="text/javascript" src="app/index.js"></script>
angular.module('myApp',[])

.factory('Data',function(){

    return {
        message : 'Hello World',
        city : [
            {
                name:'上海11212',
                py : 'shanghai'
            },
            {
                name:'北京',
                py : 'beijing'
            },
            {
                name:'四川',
                py : 'sichuan'
            }
        ]
    };
})

.controller('firstController',function($scope,Data,$filter){
   $scope.data = Data;
   // 过滤器
   var number = $filter('number')(3000);

   var jsonString = $filter('json')($scope.data);

        console.log(jsonString);
        console.log($scope.data);

   $scope.checkName = function(obj){
        if(obj.py.indexOf('h') === -1)
            return false;
        return true;
   }

})

效果:

Last updated