angular.forEach


<!DOCTYPE html>
<html lang="en" ng-app="fortest">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/angular.min.js"></script>
</head>
<body ng-controller="con">
<script>
angular.module('fortest',[]).controller('con',function($scope){
$scope.json = {
1:'apple',
2:'banana',
3:'orange',
4:'pear'
};
$scope.arr = [];
//循環json
//注意* value = 內容 key = 索引值;位置不能變但可以簡寫
angular.forEach($scope.json,function (value,key) {
//alert(key+':'+value);
//這個this是arr
this.push(key+':'+value);
},$scope.arr);
console.log($scope.arr);
$scope.arr2 = ['1:apple', '2:banana', '3:orange', '4:pear'];
$scope.json2 = {};
//循環數組
//注意* value = 內容 index = 索引值;位置不能變但可以簡寫
angular.forEach($scope.arr,function (value,index) {
//這個this是json2
this[value.split(':').slice(0,1)]=value.split(':').slice(1,2).toString();
},$scope.json2);
console.log($scope.json2);

})
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM