2013-04-10 4 views
1

데이터 트리를 조작하고 있습니다. 주어진 노드에서, RESTful 서버에서 리턴 된 하위 노드를 추가해야 할 수도 있습니다. 요청 된 노드의 완전한 교체되는 ... ... 반환각도로 자식 노드 바꾸기

[{ "text":"Apples", "id":1, "childNodes":[ 
    { "text":"first Apple", "id":'1a', "childNodes":[] }, 
    { "text":"second Apple", "id":'1b', "childNodes":[] }, 
    { "text":"third Apple", "id":'1c', "childNodes":[] } 
    ] 
}] 

을 : 데이터

[{ "text":"Apples", "id":1, "childNodes":[] }, 
{ "text":"Boxes", "id":2, "childNodes":[] }, 
{ "text":"Cups", "id":3, "childNodes":[] }] 

다음 나는 "한 ID가"의 게시물을 ... 이런 식으로 뭔가를 찾고 밖으로 시작합니다. Angular는 모델을 올바르게 레이아웃하고, 데이터 변경을 수락하고, 변경 사항을 서버에 게시하고, 마지막 JSON BLOB를 수락합니다. 굉장해. 그러나 새 데이터로 $ 범위를 업데이트하는 방법을 모르겠습니다. 내보기 ...

<div ng-repeat="node in data.nodes" > 
    <p>{{node.text}}</p> 
    <button ng-click="addLoopInstance(node)">Add child nodes</button> 
</div> 

및 컨트롤러 ...

function SurveyController($scope, sampleService) { 

    $scope.addLoopInstance = function(node) { 
     sampleService.post({ id: node.id }, function(response, getResponseHeaders) { 
      // this doesn't work 
      node = response; 
      // neither does this 
      $scope.$apply(function() { 
       node = response; 
      }); 
     }); 
    } 

} 

어떤 생각을 다음과 같이 보입니다? 미리 감사드립니다. Angular 문서는 ... 원합니다 ...하지만 사용자 기반이 매우 유용하다는 것을 알았습니다.

+0

'node.childNodes = response.childNodes'를 대신 사용 하시겠습니까? –

답변

3

그냥 당신이 말한 떨어져 가고, 나는 샘플 바이올린 내장 : 그것은 반환 않는 경우에, 그러나 이것에 대해 http://jsfiddle.net/VXxqM/3/

두 가지, 당신의 addLoopInstance()을 :

[{ "text":"Apples", "id":1, "childNodes":[ 
    { "text":"first Apple", "id":'1a', "childNodes":[] }, 
    { "text":"second Apple", "id":'1b', "childNodes":[] }, 
    { "text":"third Apple", "id":'1c', "childNodes":[] } 
    ] 
}] 

을 그런 다음을 참조해야 자식 교체하는 노드 :

node.childNodes = response[0].childNodes; 

을 그냥 단순 위해 물건을 떠나는 경우 둘째, 나도 몰라,하지만 당신은 원래 l ng-repeat 루프는 childNodes를 처리하지 않으므로, 실제로는 업데이트 중인지 확인할 방법이 없습니다. 어쨌든, 바이올린을 확인하고 내가 그 일을하기 위해 무엇을했는지보십시오.

+0

그래, 나는 내가 삽입하려고했던 수준의 응답에 혼란스러워했다. 저에게 올바른 길을 가르쳐 주셔서 감사합니다. –