1、uppercase橱脸,lowercase 大小寫轉(zhuǎn)換
{{ "lower cap string" | uppercase }}? // 結(jié)果:LOWER CAP STRING
{{ "TANK is GOOD" | lowercase }}? ? ? // 結(jié)果:tank is good
2则剃、date 格式化
{{1490161945000 | date:"yyyy-MM-dd HH:mm:ss"}} // 2017-03-22 13:52:25
3茵肃、number 格式化(保留小數(shù))
{{149016.1945000 | number:2}}
4诉儒、currency貨幣格式化
{{ 250 | currency }}? ? ? ? ? ? // 結(jié)果:$250.00
{{ 250 | currency:"RMB ¥ " }}? // 結(jié)果:RMB ¥ 250.00
5管怠、filter查找
輸入過濾器可以通過一個管道字符(|)和一個過濾器添加到指令中淆衷,該過濾器后跟一個冒號和一個模型名稱。
filter 過濾器從數(shù)組中選擇一個子集
// 查找name為iphone的行
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | filter:{'name':'iphone'} }}
6排惨、limitTo 截取
{{"1234567890" | limitTo :6}} // 從前面開始截取6位
{{"1234567890" | limitTo:-4}} // 從后面開始截取4位
7吭敢、orderBy 排序
// 根id降序排
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:'id':true }}
// 根據(jù)id升序排
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:'id' }}
以下代碼是不使用 $interval 服務(wù)的情況下,運(yùn)用 $apply暮芭,實現(xiàn)每一秒顯示信息的功能鹿驼。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.theTime = new Date().toLocaleTimeString();
$scope.setTime = function() {
// $apply 方法可以修改數(shù)據(jù)
$scope.$apply(function() {
$scope.theTime = new Date().toLocaleTimeString();
});
};
setInterval($scope.setTime, 1000);
});
$watch:持續(xù)監(jiān)聽數(shù)據(jù)上的變化,更新界面辕宏,如:
請輸入姓名:
varapp=angular.module('myApp',[]);app.controller('myCtrl',function($scope){$scope.lastName="";$scope.firstName="";//監(jiān)聽lastName的變化畜晰,更新fullName$scope.$watch('lastName',function(){$scope.fullName=$scope.lastName+" "+$scope.firstName;});//監(jiān)聽firstName的變化,更新fullName$scope.$watch('firstName',function(){$scope.fullName=$scope.lastName+" "+$scope.firstName;});});
姓:
名:
{{ lastName + " " + firstName }}
{{ fullName }}
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.lastName = "";
$scope.firstName = "";
//監(jiān)聽lastName的變化瑞筐,更新fullName
$scope.$watch('lastName', function() {
$scope.fullName = $scope.lastName + " " + $scope.firstName;
});
//監(jiān)聽firstName的變化凄鼻,更新fullName
$scope.$watch('firstName', function() {
$scope.fullName = $scope.lastName + " " + $scope.firstName;
});
});
如果通過$watch去監(jiān)聽某變量的變化,最后去更新一個fullName就太麻煩了聚假,還倒不如使用函數(shù)的方式块蚌,如getFullName():
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.lastName = "";
$scope.firstName = "";
$scope.getFullName =function(){? return $scope.lastName + " " + $scope.firstName;}
//監(jiān)聽lastName的變化,更新fullName
$scope.$watch('lastName', function() {
$scope.fullName = $scope.lastName + " " + $scope.firstName;
});
//監(jiān)聽firstName的變化膘格,更新fullName
$scope.$watch('firstName', function() {
$scope.fullName = $scope.lastName + " " + $scope.firstName;
});
});