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
angular.forEach(obj, function(value,index,objs){}, context);
| obj | Object Array | 被迭代的對象. |
| iterator | Function | 迭代函數 |
| context
(optional)
|
Object | Object to become context (this) for the iterator function. |
返回值:
對obj的引用
var heros = [{
"hero" : "曙光女神",
"role" : "sup",
"line" : "不管刮風還是下雨,太陽照常升起"}],
context = document.getElementById('context'),
arr = [];
angular.forEach(heros,function(hero,index,objs){
//value === objs[index]
angular.forEach(hero,function(value,index){
this.push(index+"-"+value);
},arr);
});
context.innetText?context.innetText = arr.join(''):context.textContent = arr.join('');
效果:
http://runjs.cn/detail/ixfp2ics
