6、AngularJs函数之大小写转换、对象深拷贝、对象扩充、数据遍历

阅读() @2018-10-29 18:16:09

今天来学习下AngularJs的函数使用方法:

1、打印AngularJs的版本号:

var m = angular.module('app', []);
m.controller('ctrl', ['$scope', function($scope){
	console.log(angular.version);
}]);

2、大小写转换:

var m = angular.module('app', []);
m.controller('ctrl', ['$scope', function($scope){
	console.log(angular.uppercase('zym'));//转成大写
	console.log(angular.lowercase('ZYM'));//转成小写
}]);

3、对象深拷贝(a对象复制b对象,a对象的值发生改变时,不影响b对象):

var m = angular.module('app', []);
m.controller('ctrl', ['$scope', function($scope){
	var obj1 = {'name':'赵一鸣WEB前端开发笔记'};
	var obj2 = {};
	angular.copy(obj1, obj2);
	obj2.name = '赵一鸣PHP学习笔记';
	console.log(obj1);//赵一鸣WEB前端开发笔记
}]);

4、对象扩充(往原对象中添加内容):

var m = angular.module('app', []);
m.controller('ctrl', ['$scope', function($scope){
	var obj3 = {'name':'赵一鸣博客'};
	angular.extend(obj3, {'url':'http://www.zymseo.com'});
	console.log(obj3);
}]);

5、使用forEach遍历对象或数组:

var m = angular.module('app', []);
m.controller('ctrl', ['$scope', function($scope){
	var arry1 = [{'name':'太原雅辉装修网'},{'url':'http://www.0351zhuangxiu.com'}];
	angular.forEach(arry1, function(value, key){
		console.log(value);
	});

	var obj4 = {'name':'zym', 'work':'web前端开发'};
	angular.forEach(obj4, function(value, key){
		console.log(key);
	});
}]);

持续更新中!

微信二维码