Home  >  Article  >  Web Front-end  >  angular.foreach loop method usage guide_AngularJS

angular.foreach loop method usage guide_AngularJS

WBOY
WBOYOriginal
2016-05-16 16:21:581336browse

angular has its own life cycle. When looping to a variable monitored by angular. It is best to use the loop method that comes with Angular. "angular.foreach"

Format:

Copy code The code is as follows:

var objs =[{a:1},{a:2}];
angular.forEach(objs, function(data,index,array){
//data is equivalent to array[index]
console.log(data.a '=' array[index].a);
});

The parameters are as follows:

objs: Collection that needs to be traversed

data: current data during traversal

index: current index when traversing

array: The collection that needs to be traversed, objs will be passed as it is every time it is traversed.

You don’t need to write the last two parameters:

Copy code The code is as follows:

var objs =[{a:1},{a:2}];
angular.forEach(objs, function(data){
console.log(data.a);
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn