データツリーを操作しています。特定のノードから、RESTful サーバーから返される子ノードを追加する必要がある場合があります。データは次のように始まります…</p>
[{ "text":"Apples", "id":1, "childNodes":[] },
{ "text":"Boxes", "id":2, "childNodes":[] },
{ "text":"Cups", "id":3, "childNodes":[] }]
次に、返される「id:1」の投稿を行います…</p>
[{ "text":"Apples", "id":1, "childNodes":[
{ "text":"first Apple", "id":'1a', "childNodes":[] },
{ "text":"second Apple", "id":'1b', "childNodes":[] },
{ "text":"third Apple", "id":'1c', "childNodes":[] }
]
}]
…これは、要求されたノードの完全な置き換えです。Angular でモデルを適切にレイアウトし、データの変更を受け入れ、それらの変更をサーバーに投稿し、最後の JSON BLOB を受け入れます。素晴らしい。しかし、新しいデータで $scope を更新する方法がわかりません。私のビューは次のようになります…</p>
<div ng-repeat="node in data.nodes" >
<p>{{node.text}}</p>
<button ng-click="addLoopInstance(node)">Add child nodes</button>
</div>
そしてコントローラー…</p>
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 のドキュメントは… 欲しい… しかし、私はユーザー ベースが非常に役立つことを発見しました。