2016-05-22 148 views
0

我是angularjs的新手,我想知道如何实现下面的示例。动态添加新元素并在选择的元素上添加新的子元素angularjs

第一节 - 子第1节 - 子第2节 - 子第3节

第2节 - 子第1节 - 子第2节

在哪里用户可以添加动态地更多的部分,并且从该部分他/她可以动态地添加更多的子部分。另外,我怎样才能得到特定元素的索引。希望我解释得很好。感谢提前:)

enter image description here

回答

1

可能有许多方法可以做到这一点,他们中的一个可能是初始化父章节子段的阵列和其他。孩子会保持知道他的父母是谁。

见工作plunker这里:Demo

两个数组:

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

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

查看:

<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