2016-09-29 4 views
-2

JSON 응답이 아래에 있습니다. 선택한 역할에 따라 역할 정보를 채울 필요가 있습니다. 역할 정보를 roleInfo 배열로 옮겼지만 문제는 키와 값을 채울 수 없습니다. 그에 따라 테이블에 값을 얻을 수 있지만 테이블에 채울 수는 없습니다. 키와 그 값을 테이블로 옮길 수 없었습니다. QA와 개발을 하위 키 화이트 박스 테스트 및 블랙 박스 테스트로 이동해야합니다. 내 for 루프가 종료 될 때 오류가 발생합니다.각도 js의 for 루프 처리

JSON :

{ 
     "json": { 
     "response": { 
      "servicetype": "1", 
      "functiontype": "10011", 
      "statuscode": "0", 
      "statusmessage": "Success", 
      "data": { 
      "unassignedroles": [ 
       {"rolename":"1", 
       "roleinformation": { 
        "QA": [ 
        { 
         "White Box Testing": 0 
        }, 
        { 
        "Black Box Testing": 10 
        } 

        ], 
         "Development": [ 
        { 
         "White Box Testing": 0 
        }, 
        { 
        "Black Box Testing": 10 
        } 

        ] 
       } 
       }, 
       { 
`    "rolename":"2"`, 
       "roleinformation": { 
        "1": [ 
        { 
         "A": 0 
        } 
        ] 
       } 
       } 

JS :

 var roleInfo = []; 
     UserService.getAssignRoles(json).then(function(response) { 

if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success') 
{ 
    $scope.model.assignroles = [], assignrolesArray = []; 

    var unasresdata = response.json.response.data.unassignedroles; 
    var assresdata = response.json.response.data.assignedtoles; 

    assignrolesArray = unasresdata.concat(assresdata); 

    $scope.model.assignroles = assignrolesArray; 
     for(var i = 0; i < assignrolesArray.length ; i++){ 

     if (($scope.model.rolename === assignrolesArray[i].rolename) && (assignrolesArray[i].rolename !== undefined)){ 
      roleInfo = assignrolesArray[i].roleinformation; 
      for (i in roleInfo) 

    }  
    } 

    } 
    }); 

HTML :

<select class="form-control" name="role" 
     ng-model="model.rolename" 
     ng-change="getAssignRole(data,model.rolename)"> 
     <option selected>Select Roles</option> 
     <option ng-repeat="role in model.assignroles track by $index" 
     value="{{role.rolename}}">{{role.rolename}}</option> 
</select>  
<div>   
     <table class="smalltable"> 
      <thead> 
       <tr ng-repeat="i(key, value) in roleInfo"> 
        <td>{{ key }}</td> 
        <td ng-repeat="details in value"> 
    `     <p ng-repeat"(subkey, subvalue) in details">{{ subkey }} : {{ subvalue }}</p> 
        </td> 
       </tr> 
      </thead> 
     </table> 
    </div> 
+1

당신은 그것을 위해 plunker를 추가 할 수 있습니까? –

+0

안녕하세요, REST API를 통해이 응답을받습니다. 나는 두려워하지 않을 수 없다. –

+0

JS 코드 솔기가 불완전합니다. 'for (i in roleInfo)'에 내용이 없습니다 ... – Daniel

답변

0

사용 angular.extend()가

$scope.roleInfo={}; 

for(var i = 0; i < assignrolesArray.length ; i++){ 
      $scope.roleInfo = angular.extend($scope.roleInfo,assignrolesArray[i].roleinformation);   

    } 
012 roleInfo

의 오브젝트를 concate 할 3,516,

는 아래의 코드를 참조 plunker link

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="myCtrl"> 

<div>  
     <table> 
      <thead> 
       <tr ng-repeat="(key,value) in roleInfo"> 
        <td>{{ key }}</td> 
        <td ng-repeat="details in value"> 
         <p ng-repeat="(subkey, subvalue) in details"> 
          {{subkey}}:{{subvalue}} 
         </p> 
        </td> 


       </tr> 
      </thead> 
     </table> 
    </div> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) { 

var response = 
{ 
    "json": { 
     "response": { 
      "servicetype": "1", 
      "functiontype": "10011", 
      "statuscode": "0", 
      "statusmessage": "Success", 
      "data": { 
       "unassignedroles": [{ 
        "rolename": "1", 
        "roleinformation": { 
         "QA": [{ 
           "White Box Testing": 0 
          }, { 
           "Black Box Testing": 10 
          } 

         ], 
         "Development": [{ 
           "White Box Testing": 0 
          }, { 
           "Black Box Testing": 10 
          } 

         ] 
        } 
       }, { 
        "rolename": "2", 
        "roleinformation": { 
         "1": [{ 
          "A": 0 
         }] 
        } 
       }] 

      } 
     } 
    } 
}; 

var unasresdata = response.json.response.data.unassignedroles; 
var assresdata = [{ 
        "rolename": "1a", 
        "roleinformation": { 
         "QA": [{ 
           "White Box Testing": 0 
          }, { 
           "Black Box Testing": 10 
          } 

         ], 
         "Development": [{ 
           "White Box Testing": 0 
          }, { 
           "Black Box Testing": 10 
          } 

         ] 
        } 
       }, { 
        "rolename": "2a", 
        "roleinformation": { 
         "1": [{ 
          "A": 0 
         }] 
        } 
       }]; 

    var assignrolesArray = unasresdata.concat(assresdata); 

    console.log("This is assignedrolesArray : "+ assignrolesArray); 
    $scope.roleInfo={}; 

for(var i = 0; i < assignrolesArray.length ; i++){ 
      $scope.roleInfo = angular.extend($scope.roleInfo,assignrolesArray[i].roleinformation);   

    } 



}); 
</script> 



</body> 
</html>