记录学习AngularJS

1、uppercase,lowercase 大小写转换

{{ "lower cap string" | uppercase }}  // 结果:LOWER CAP STRING

{{ "TANK is GOOD" | lowercase }}      // 结果:tank is good

2、date 格式化

{{1490161945000 | date:"yyyy-MM-dd HH:mm:ss"}} // 2017-03-22 13:52:25

3、number 格式化(保留小数)

{{149016.1945000 | number:2}}

4、currency货币格式化

{{ 250 | currency }}            // 结果:$250.00

{{ 250 | currency:"RMB ¥ " }}  // 结果:RMB ¥ 250.00

5、filter查找

输入过滤器可以通过一个管道字符(|)和一个过滤器添加到指令中,该过滤器后跟一个冒号和一个模型名称。

filter 过滤器从数组中选择一个子集

// 查找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 }}

// 根据id升序排

{{ [{"age": 20,"id": 10,"name": "iphone"},

{"age": 12,"id": 11,"name": "sunm xing"},

{"age": 44,"id": 12,"name": "test abc"}

] | orderBy:'id' }}

以下代码是不使用 $interval 服务的情况下,运用 $apply,实现每一秒显示信息的功能。

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {

$scope.theTime = new Date().toLocaleTimeString();

$scope.setTime = function() {

// $apply 方法可以修改数据

$scope.$apply(function() {

$scope.theTime = new Date().toLocaleTimeString();

});

};

setInterval($scope.setTime, 1000);

});

$watch:持续监听数据上的变化,更新界面,如:

请输入姓名:

varapp=angular.module('myApp',[]);app.controller('myCtrl',function($scope){$scope.lastName="";$scope.firstName="";//监听lastName的变化,更新fullName$scope.$watch('lastName',function(){$scope.fullName=$scope.lastName+" "+$scope.firstName;});//监听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 = "";

//监听lastName的变化,更新fullName

$scope.$watch('lastName', function() {

$scope.fullName = $scope.lastName + " " + $scope.firstName;

});

//监听firstName的变化,更新fullName

$scope.$watch('firstName', function() {

$scope.fullName = $scope.lastName + " " + $scope.firstName;

});

});

如果通过$watch去监听某变量的变化,最后去更新一个fullName就太麻烦了,还倒不如使用函数的方式,如getFullName():

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {

$scope.lastName = "";

$scope.firstName = "";

$scope.getFullName =function(){  return $scope.lastName + " " + $scope.firstName;}

//监听lastName的变化,更新fullName

$scope.$watch('lastName', function() {

$scope.fullName = $scope.lastName + " " + $scope.firstName;

});

//监听firstName的变化,更新fullName

$scope.$watch('firstName', function() {

$scope.fullName = $scope.lastName + " " + $scope.firstName;

});

});

你可能感兴趣的:(记录学习AngularJS)