AngularJs函数之大小写转换、对象深拷贝、对象扩充、数据遍历(代码实例)
程序员文章站
2022-04-24 18:14:13
1、打印AngularJs的版本号:
1 var m = angular.module('app', []);
2 m.controller('ct...
1、打印AngularJs的版本号:
1 var m = angular.module('app', []); 2 m.controller('ctrl', ['$scope', function($scope){ 3 console.log(angular.version); 4 }]);
2、大小写转换:
1 var m = angular.module('app', []); 2 m.controller('ctrl', ['$scope', function($scope){ 3 console.log(angular.uppercase('zym'));//转成大写 4 console.log(angular.lowercase('ZYM'));//转成小写 5 }]);
3、对象深拷贝(a对象复制b对象,a对象的值发生改变时,不影响b对象):
1 var m = angular.module('app', []); 2 m.controller('ctrl', ['$scope', function($scope){ 3 var obj1 = {'name':'K`illCodeWEB前端开发笔记'}; 4 var obj2 = {}; 5 angular.copy(obj1, obj2); 6 obj2.name = 'K`IllcodePHP学习笔记'; 7 console.log(obj1); 8 }]);
4、对象扩充(往原对象中添加内容):
1 var m = angular.module('app', []); 2 m.controller('ctrl', ['$scope', function($scope){ 3 var obj3 = {'name':'K`illCode客'}; 4 angular.extend(obj3, {'url':'http://www.zymseo.com'}); 5 console.log(obj3); 6 }]);
5、使用forEach遍历对象或数组:
01 var m = angular.module('app', []); 02 m.controller('ctrl', ['$scope', function($scope){ 03 var arry1 = [{'name':'太原雅辉装修网'},{'url':'http://www.0351zhuangxiu.com'}]; 04 angular.forEach(arry1, function(value, key){ 05 console.log(value); 06 }); 07 08 var obj4 = {'name':'zym', 'work':'web前端开发'}; 09 angular.forEach(obj4, function(value, key){ 10 console.log(key); 11 }); 12 }]);