angularJs 之forEach用法遍历复杂Json

<!DOCTYPE html>
<html ng-app="test">
<head>
    <title>angularJs-forEach</title>
</head>
<body>
    <div ng-controller="ForEachCtrl">
    <div>{{text1}}</div>
    <div>{{text2}}</div>
    <div>{{text3}}</div>
</div>  

<script src="//www.w3cschool.cc/try/angularjs/1.2.5/angular.min.js"></script>
<script type="text/javascript">
        var test = angular.module('test', []);
        test.controller('ForEachCtrl', function($scope){
        var arr = [{'name':'diamping','items':[{'name':'A', 'value':'B','ss': 'C'},
                {'name':'F', 'value':'D','ss': 'C'}],'checked':1},
                {'name':'mwiwei','items':[{'name':'A', 'value':'B','ss': 'C'},
                {'name':'F', 'value':'D','ss': 'C'}],'checked':1}];
            var text1 =[];//第1种
            var text2 =[];//第2种
            var text3 =[];//第3种
            
            angular.forEach(arr, function(a) {
               $scope.arrays= a;
               angular.forEach($scope.arrays.items, function(item) {
                    if(item.checked==1){
                       //alert(angular.toJson(item.name));
                       text1.push(item.name+ ":" + item.value);  
                       text2.push(item.name+ ":" + item.value);
                       text3.push(item.name+ ":" + item.value);                 
               });                                           
           });
            $scope.text1 = text1.join(',');//第1种
            $scope.text2 = angular.toJson(text2);//第2种
            $scope.text3 = angular.toJson(text3.join(','));//第3种
            
        })
</script>
</body>
</html>


你可能感兴趣的:(Angular,foreach)