欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS變量及過濾器Filter用法分析

 更新時(shí)間:2016年11月22日 10:37:19   作者:栁羅風(fēng)塵  
這篇文章主要介紹了AngularJS變量及過濾器Filter用法,結(jié)合實(shí)例形式分析了AngularJS變量、過濾器及自定義過濾器的相關(guān)用法與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了AngularJS變量及過濾器Filter用法。分享給大家供大家參考,具體如下:

1. 關(guān)于部分變量的操作

設(shè)置變量:

ng-init="hour=14" //設(shè)置hour變量在DOM中 使用data-ng-init 更好些
$scope.hour = 14; //設(shè)置hour變量在js中

使用變量:

(1) 如果是在DOM 相關(guān)的 ng-*** 屬性里 直接寫變量名

如:

<p ng-show="hour > 13">I am visible.</p>

(2) 如果是在控制器HTML 中但是不在 ng屬性里

使用{{變量名}}

如:

{{hour}}

(3) 當(dāng)然第三種就是上面的 在js中使用

加上對(duì)象名 $scope.

$scope.hour

(4) 在表單控件中 ng-model中的變量 可以直接

同時(shí)在 html 中 使用 {{變量名}}

<p>Name: <input type="text" ng-model="name"></p>
<p>You wrote: {{ name }}</p>

還可以通過 ng-bind 屬性進(jìn)行變量綁定

<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>

(5) 可以直接在ng的屬性 或變量中使用表達(dá)式

會(huì)自動(dòng)幫你計(jì)算 需要符合js語法

ng-show="true?false:true"
{{5+6}}
<div ng-app="" ng-init="points=[1,15,19,2,40]">
  <p>The third result is <span ng-bind="points[2]"></span></p>
</div>

2. js中的變量循環(huán)

<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
 <ul>
  <li ng-repeat="x in names">
   {{ x }}
  </li>
 </ul>
</div>

3.變量的過濾 filter

Filter         Description
currency    以金融格式格式化數(shù)字
filter          選擇從一個(gè)數(shù)組項(xiàng)中過濾留下子集。
lowercase   小寫
orderBy     通過表達(dá)式將數(shù)組排序
uppercase   大寫

如:

<p>The name is {{ lastName | uppercase }}</p>

當(dāng)然多個(gè)函數(shù)封裝可以使用 |

<p>The name is {{ lastName | uppercase | lowercase }}</p>
//排序函數(shù)的使用
<ul>
 <li ng-repeat="x in names | orderBy:'country'">
  {{ x.name + ', ' + x.country }}
 </li>
</ul>
//通過輸入內(nèi)容自動(dòng)過濾顯示結(jié)果
<div ng-app="" ng-controller="namesCtrl">
  <p><input type="text" ng-model="test"></p>
  <ul>
   <li ng-repeat="x in names | filter:test | orderBy:'country'">
    {{ (x.name | uppercase) + ', ' + x.country }}
   </li>
  </ul>
</div>

names | filter:test | orderBy:'country'
就是將names數(shù)組中的項(xiàng) 按照 test表單值進(jìn)行 篩選
然后以 names中的子元素 country 進(jìn)行排序

自定義過濾器:

<!DOCTYPE html>
<html ng-app="HelloApp">
<head>
<title></title>
</head>
<body ng-controller="HelloCtrl">
 <form>
   <input type="text" ng-model="name"/>
 </form>
 <div>{{name|titlecase}}</div>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <script type="text/javascript">
  // 編寫過濾器模塊
  angular.module('CustomFilterModule', [])
      .filter( 'titlecase', function() {
    return function( input ) {
      return input.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  }
  });
  // 實(shí)際展示模塊
  // 引入依賴的過濾器模塊 CustomFilterModule
  angular.module('HelloApp', [ 'CustomFilterModule'])
    .controller('HelloCtrl', ['$scope', function($scope){
    $scope.name = '';
  }])
</script>
</body>
</html>

希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論