angular.forEach

angular.forEach
描述:
循环对obj对象的每个元素调用iterator, obj对象可以是一个Object或一个Array. Iterator函数调用方法: iterator(value, key, obj), 其中obj是被迭代对象,key是obj的property key或者是数组的index,value就是相应的值啦. (此函数不能够迭代继承的属性.)
使用方法:
angular.forEach(obj, iterator, [context])
参数详解:
Param Type Details
obj ObjectArray

被迭代的对象.

iterator Function

迭代函数

context
(optional)
Object

Object to become context (this) for the iterator function.

返回值:
对obj的引用
实例代码:
<!DOCTYPE HTML>
<html ng-app="elementExample">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="angular.min.js"></script>
</head>

<body>
<div id="test">博弈网络科技</div>

<div>
angular.forEach(obj, iterator, context)
<input type="button" onclick="button1()" value="比较值"></input>
</div>
<hr>
<div>
angular.forEach(obj, iterator)
<input type="button" onclick="button2()" value="比较值"></input>
</div>
<br>

<script>

	function button1(){
		var values = {name: 'misko', gender: 'male'};
		var log = [];
		angular.forEach(values, function(value, key) {
		  this.push(key + ': ' + value);
		}, log);
		alert(log);
		
	}
	
	function button2(){
		var values = {name: 'misko', gender: 'male'};
		var log = [];
		angular.forEach(values, function(value, key) {
		  log.push(key + ': ' + value);
		});
		alert(log);
		
	}

</script>

</body>	
</html>
 

你可能感兴趣的:(AngularJS,AngularJS API,angular.forEach)