2014-11-26 2 views
1

개체가 포함 된 변수 키 이름을 가진 개체가 있습니다. 개체에 대해 깊이 우선 검색 (DFS)을하고 싶습니다.중첩 된 ng-repeat를 사용하여 깊이 우선 검색

예 개체 :

object = { "var1" : { 
        "var2a" : { 
          "someKey" : someValue 
          }, 
        "var2b" : { 
          "someKey" : someOtherValue 
          }}} 
다음과 같이 DFS에서 내 시도가 (작동하지 않을 것이다)입니다

:

<div ng-repeat = "child in object"> 
    <div ng-repeat = "grandChild in child "> 
      <td> {{grandChild.aKey}}</td> 
    </div> 
</div> 

내가 다른 사람 in this previous question의 접근을 보았다, 그러나 이것은하지 않습니다 변수 키 이름 때문에 저에게 도움이됩니다.

또한 개체의 깊이도 알려져 있습니다.

우리는 child의 결과가 무엇인지 확인하려면이 코드를 사용하는 경우 :

{"var2a" : { 
      "someKey" : someValue 
      }} 

편집 : : 그것은 할 수있을 수

<div ng-repeat = "child in object"> 
    <td> {{child}}</td> 
    <div ng-repeat = "grandChild in child "> 
      <td> {{grandChild.aKey}}</td> 
    </div> 
</div> 

우리는 child의 첫 번째 인스턴스가 될 수 콘솔로 출력 할이 JavaScript DFS 스 니펫을 활용 하시겠습니까?

angular.forEach(object,function(child,value){ 
     angular.forEach(child,function(grandChild,value){ 
      console.log(grandChild.someKey) 
     }); 
}); 

답변

1

따라서 실제로 중첩 된 ng-Repeats를 사용할 수없는 것으로 나타났습니다. 자바 스크립트로 작성해야합니다. 이것은 실제로 HTML 파일에서 논리를 유지하는 'Angular JS'정신과 일치하여 작동합니다. 여기

자바 스크립트에서 DFS입니다 :

angular.forEach(object,function(key,value){ 

       if (value!='$id'){ 
          angular.forEach(key,function(keyChild,valueChild){ 

            console.log(valueChild) 
          } 
       }); 
    }); 

이 반환됩니다 "someKey" : someOtherValue"someKey" : someOtherValue