フィルタは、データをフォーマットするために AngularJS に追加することができます。
AngularJS は、データを変換するためのフィルタを提供します:
currency
数値を通貨形式にフォーマットする。date
日付を指定の形式にフォーマットする。filter
配列から項目のサブセットを選択する。json
オブジェクトを JSON 文字列にフォーマットする。limitTo
指定した数の要素/文字に、配列/文字列を制限する。lowercase
文字列を小文字にフォーマットする。number
数値を文字列にフォーマットする。orderBy
式で配列を整列する。uppercase
文字列を大文字にフォーマットする。パイプ文字 |
と、その後にフィルタを続けることで、式にフィルタを追加することができます。
uppercase
フィルタは、文字列を大文字にフォーマットします:
<div ng-app="myApp" ng-controller="personCtrl">
<p>The name is {{ lastName | uppercase }}</p>
</div>
Try it Yourself »
lowercase
フィルタは、文字列を小文字にフォーマットします:
<div ng-app="myApp" ng-controller="personCtrl">
<p>The name is {{ lastName | lowercase }}</p>
</div>
Try it Yourself »
パイプ文字 |
と、その後にフィルタを続けることで、ng-repeat
のようなディレクティブへ
フィルタが追加されます:
orderBy
フィルタは、配列をソートします:
<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
<li ng-repeat="x in names | orderBy:'country'">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
Try it Yourself »
currency
フィルタは、数値を通貨にフォーマットします:
<div ng-app="myApp" ng-controller="costCtrl">
<h1>Price: {{ price | currency }}</h1>
</div>
Try it Yourself »
currency フィルタの詳細に関しては、AngularJS currency フィルタ・リファレンス をご覧ください。
filter
フィルタは、配列の部分配列を選択します。
filter
フィルタは、配列でにみ使用することができるもので、一致する項目だけを含む配列を返します。
文字 "i" が含まれる名前を返します:
<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
<li ng-repeat="x in names |
filter : 'i'">
{{ x }}
</li>
</ul>
</div>
Try it Yourself »
filter フィルタの詳細に関しては、AngularJS filter フィルタ・リファレンス をご覧ください。
入力フィールドに ng-model
ディレクティブを設定することにより、
フィルタ内の式として入力フィールドの値を使用することができます。
入力フィールドに文字を入力すると、一致に応じてリストが伸縮します:
<div ng-app="myApp" ng-controller="namesCtrl">
<p><input type="text" ng-model="test"></p>
<ul>
<li ng-repeat="x in names | filter : test">
{{ x }}
</li>
</ul>
</div>
Try it Yourself »
ソート順を変更するには、テーブルヘッダをクリックしてください:
Name | Country |
---|---|
{{x.name}} | {{x.country}} |
テーブルのヘッダに ng-click
ディレクティブを追加することで、配列のソート順を変更する関数を実行することができます:
<div ng-app="myApp" ng-controller="namesCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('name')">Name</th>
<th ng-click="orderByMe('country')">Country</th>
</tr>
<tr
ng-repeat="x in names | orderBy:myOrderBy">
<td>{{x.name}}</td>
<td>{{x.country}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl',
function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy
= x;
}
});
</script>
Try it Yourself »
モジュールにより、新規 filter factory 関数を登録することで、独自のフィルタを作ることができます:
Make a custom filter called "myFormat":
<ul ng-app="myApp" ng-controller="namesCtrl">
<li ng-repeat="x
in names">
{{x |
myFormat}}
</li>
</ul>
<script>
var app =
angular.module('myApp', []);
app.filter('myFormat',
function() {
return function(x) {
var i, c, txt = "";
for (i = 0; i <
x.length; i++) {
c = x[i];
if (i % 2 == 0) {
c = c.toUpperCase();
}
txt
+= c;
}
return txt;
};
});
app.controller('namesCtrl', function($scope)
{
$scope.names = ['Jani', 'Carl', 'Margareth', 'Hege',
'Joe', 'Gustav', 'Birgit', 'Mary', 'Kai'];
});
</script>
Try it Yourself »
myFormat
フィルタは、他のすべての文字を大文字にフォーマットします。