2016-05-22 20 views
0

私はangularjsを初めて使用しています。新しい要素を動的に追加し、anglejsで選択した要素に新しいサブ要素を追加します。

項1 - サブセクション1 - サブセクション2 - サブセクション3

第2 - サブセクション1 - サブセクションユーザーが追加できる2

より多くのセクションを動的に作成し、そのセクションから、より多くのサブセクションを動的に追加することができます。また、特定の要素のインデックスを取得する方法もあります。私はそれを正しく説明したいと思っています。これを行うには、おそらく多くの方法があります。事前に感謝:)

enter image description here

答えて

1

、そのうちの一つは、子セクションの親セクションの配列や他を初期化することができます。子供はその親が誰であるかについての知識を保持するだろう。

は、ここで作業plunkerを参照してください:Demo

2個のアレイ:

$scope.section = [{id: 1}, {id:2}]; 

$scope.subSection = [{id:1, parentId: 1}, {id:2, parentId: 1},{id:3, parentId: 2}]; 

ビュー:Devのワン@

<div ng-repeat="parent in section"> 
    <span>Section {{parent.id}}</span> 
    <pre> 
    <div ng-repeat="child in subSection" ng-if="child.parentId == parent.id"> 
     <span>SubSection {{child.id}}</span> 
    </div> 
     <button ng-click="addSubSection($index + 1)">Add SubSection</button> 
    </pre> 
</div> 
<button ng-click="addSection()">Add Section</button> 
+0

感謝を。ほんとうにありがとう。これは私が探しているものです。 – jamcoder

0
<div ng-repeat="section in sections"> 
    {{ section.name }} 
    <div ng-repeat="subsection in section.subsections"> 
    {{ subsection.name }} 
    </div> 
    <button ng-click="addSubsectionTo(section)">Add subsection</button> 
</div> 
<button ng-click="addSection()">Add section</button> 
+0

ありがとう@JB Nizet – jamcoder

関連する問題