溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

angularjs-$filter及callee,$watch

發(fā)布時(shí)間:2020-05-28 08:17:14 來(lái)源:網(wǎng)絡(luò) 閱讀:282 作者:f1yinsky 欄目:web開(kāi)發(fā)
#angularjs常用過(guò)濾器
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <script src="js/angular.min.js"></script>
 </head>
 <body>
  <div ng-app="app" ng-controller="ctrl">
   貨幣currency:{{999.99|currency:'$':1}} <br />
   數(shù)字number:{{999.111|number:2}} <br />
   大寫(xiě)lowercase:{{"CXIONG"|lowercase}} <br />
   小寫(xiě)uppercase: {{"cxiong"|uppercase}} <br />
   截取字符limitTo:{{"cxiong"|limitTo:2:1}} <br />
   日期data:{{time|date:'yyyy年MM月dd日 HH時(shí)mm分ss秒'}} <br />
   排序orderBy:{{data|orderBy}} <br />
   <!--filter精確匹配-->
   過(guò)濾filter:{{data|filter:1:true}} <br />
  </div>

  <script type="text/javascript"> 
   var m=angular.module('app',[]);
   m.controller('ctrl',['$scope',function($scope){
     $scope.time=new Date().getTime()
     $scope.data=[1,3,4,55,66,23,14,41]
    }]); 
   
  </script>
 </body>
</html>
#$filter和callee應(yīng)用表格排序
<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript" src='js/angular.min.js'> </script>
  <style type="text/css">
   .ng-cloak{display:none;}
  </style>
 </head>
 <body ng-app="hd" ng-cloak class="ng-cloak">
  <div ng-controller="ctrl">
   <table border="" cellspacing="" cellpadding="">
    <tr>
     <th ng-click="sort('name')">名稱</th>
     <th ng-click="sort('num')">數(shù)量</th>
     <th ng-click="sort('price')">價(jià)格</th>
    </tr>
    <tr ng-repeat="d in data">
     <td>`d`.`name`</td>
     <td>`d`.`num`</td>
     <td>`d`.`price`</td>
    </tr>
   </table>
  </div>
  
  <script type="text/javascript">
   var m=angular.module('hd',[])
   m.controller('ctrl',['$scope','$filter',function($scope,$filter){
    $scope.data=[
     {'name':'iphone6','num':150,'price':3999},
     {'name':'beats','num':100,'price':999},
     {'name':'iphone7','num':500,'price':5999},
     {'name':'ipad','num':250,'price':1999}
    ]
    var status=true
    $scope.sort=function(field){
     //arguments.callee 全局存放靜態(tài)變量
     if (arguments.callee[field]=='undefine') {
      arguments.callee[field]=true
     }
     arguments.callee[field]=!arguments.callee[field]
     $scope.data=$filter('orderBy')($scope.data,field,arguments.callee[field])
    }
    
   }])
  </script>
 </body>
</html>
#全局變量保存狀態(tài)
<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript" src='js/angular.min.js'> </script>
  <style type="text/css">
   .ng-cloak{display:none;}
  </style>
 </head>
 <body ng-app="hd" ng-cloak class="ng-cloak">
  <div ng-controller="ctrl">
   <table border="1" cellspacing="" cellpadding="">
    <tr>
     <th ng-click="sort('name')">名稱</th>
     <th ng-click="sort('num')">數(shù)量 `status`
      <span ng-if="status">升序</span>       
      <span ng-if="!status">降序</span> 
     </th>
     <th ng-click="sort('price')">價(jià)格</th>
    </tr>
    <tr ng-repeat="d in data">
     <td>`d`.`name`</td>
     <td>`d`.`num`</td>
     <td>`d`.`price`</td>
    </tr>
   </table>
  </div>
  
  <script type="text/javascript">
   var m=angular.module('hd',[])
   m.controller('ctrl',['$scope','$filter',function($scope,$filter){
    $scope.data=[
     {'name':'iphone6','num':150,'price':3999},
     {'name':'beats','num':100,'price':999},
     {'name':'iphone7','num':500,'price':5999},
     {'name':'ipad','num':250,'price':1999}
    ]
    $scope.status=false
    $scope.sort=function(field){
     //arguments.callee 全局存放靜態(tài)變量
//     if (arguments.callee[field]=='undefine') {
//      arguments.callee[field]=true
//     }
//     arguments.callee[field]=!arguments.callee[field]     
//     $scope.data=$filter('orderBy')($scope.data,field,arguments.callee[field])
     $scope.status=!$scope.status
     $scope.data=$filter('orderBy')($scope.data,field,$scope.status)
    }
    
   }])
  </script>
 </body>
</html>
#$watch監(jiān)控某個(gè)變量
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <script src="js/angular.min.js"></script>
 </head>
 <body>
  <div ng-app="app" ng-controller="ctrl">
   $watch: <input type="text" ng-model="title"/>`err`
  </div>

  <script type="text/javascript"> 
   var m=angular.module('app',[]);
   m.controller('ctrl',['$scope',function($scope){
     $scope.title=''
     //n為當(dāng)前輸入字符,o為上一次字符
     $scope.$watch('title',function(n,o){
      $scope.err=n.length>0?'':'不能為空';
     })
    }]); 
   
  </script>
 </body>
</html>
#$watch監(jiān)控某個(gè)對(duì)象
<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript" src='js/angular.min.js'> </script>
  <style type="text/css">
   .ng-cloak{display:none;}
  </style>
 </head>
 <body ng-app="hd" ng-cloak class="ng-cloak">
  <div ng-controller="ctrl">
   請(qǐng)輸入姓名:<input type="text" ng-model="odata.name"/>`err`
  </div>
  
  <script type="text/javascript">
   var m=angular.module('hd',[])
   m.controller('ctrl',['$scope','$filter',function($scope,$filter){
   
    $scope.odata={'name':'cxiong','age':29};
    $scope.$watch('odata.name',function(n,o){
     $scope.err=n.length?'':'不能為空';
    });
   }])
  </script>
 </body>
</html>
#$watch和$filter實(shí)現(xiàn)排序和搜索框功能,自定義加*過(guò)濾器
<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript" src='js/angular.min.js'> </script>
  <style type="text/css">
   .ng-cloak{display:none;}
  </style>
 </head>
 <body ng-app="hd" ng-cloak class="ng-cloak">
  <div ng-controller="ctrl">
   搜索框:<input type="text" ng-model="search"/>
   <table border="1" cellspacing="" cellpadding="">
    <tr>
     <th ng-click="sort('name')">名稱</th>
     <th ng-click="sort('num')">數(shù)量
      <span ng-if="status">升序</span>       
      <span ng-if="!status">降序</span> 
     </th>
     <th ng-click="sort('price')">價(jià)格</th>
    </tr>
    <tr ng-repeat="d in tmp">
     <td>`d`.`name`</td>
     <td>`d`.`num`</td>
     <td>{{d.price|truncate}}</td>
    </tr>
   </table>
  </div>
  
  <script type="text/javascript">
   var m=angular.module('hd',[]);
   //自定義加*過(guò)濾器
   m.filter('truncate',function(){
    return function(price){
     return String(parseInt(price/100))+'**元'
    }
   })
   
   m.controller('ctrl',['$scope','$filter',function($scope,$filter){
    $scope.data=[
     {'name':'iphone6','num':150,'price':3999},
     {'name':'beats','num':100,'price':999},
     {'name':'iphone7','num':500,'price':5999},
     {'name':'ipad','num':250,'price':1999}
    ]
    
    //排序功能
    $scope.status=false
    $scope.sort=function(field){
     //arguments.callee 全局存放靜態(tài)變量
//     if (arguments.callee[field]=='undefine') {
//      arguments.callee[field]=true
//     }
//     arguments.callee[field]=!arguments.callee[field]     
//     $scope.data=$filter('orderBy')($scope.data,field,arguments.callee[field])
     $scope.status=!$scope.status
     
     $scope.tmp=$filter('orderBy')($scope.data,field,$scope.status)
    }
    
    //搜索框功能
    //過(guò)濾后數(shù)據(jù)。用于顯示
    $scope.tmp=$scope.data
    $scope.$watch('search',function(n,o){
     $scope.tmp=$filter('filter')($scope.data,n)
    })
   }])
  </script>
 </body>
</html>


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI