का उपयोग कर निर्देशों मैं एक treeview AngularJS का उपयोग कर बनाने के लिए कोशिश कर रहा हूँ।रिकर्सिव कस्टम ngRepeat
यहाँ मेरी कोड है:
module.directive('treeview', function() {
return {
restrict: 'E',
templateUrl: "/templates/ui/controls/treeview.htm",
replace: true,
transclude: true,
scope: {},
link: function (scope, element, attrs) {
console.log("treeview directive loaded");
},
controller: function ($scope, $rootScope) {
$rootScope.depth = 0;
$scope.items = [
{ text: "face" },
{ text: "palm" },
{
text: "cake",
childitems: [
{ text: "1 face" },
{ text: "1 palm" },
{ text: "1 cake" }
]
}
];
}
};
});
module.directive('treeviewItem', function() {
return {
restrict: 'E',
templateUrl: "/templates/ui/controls/treeview-item.htm",
replace: true,
scope: {
item: "="
},
link: function (scope, element, attrs) {
console.log("treeview item directive loaded");
}
};
});
Treeview टेम्पलेट:
<div class="sl-treeview">
<ul class="clear" ng-transclude>
<treeview-item ng-repeat="item in items" item="item"></treeview-item>
</ul>
</div>
Treeview आइटम टेम्पलेट:
<li>
<i class="icon-plus-sign"></i>
<a href="/">
<i class="icon-folder-close"></i>
{{item.text}}
</a>
<!-- This ul is the issue - it crashes the page -->
<ul>
<treeview-item ng-repeat="childitem in item.childitems" item="childitem"></treeview-item>
</ul>
</li>
treeview निर्देश $scope.items
में विकास के लिए hardcoded है - अंत में मैं इस आशा सेवा से डेटा खींचने वाले नियंत्रक/सेवा से आएगा आर। हालांकि यह उस मूल संरचना के प्रकार का प्रतिनिधित्व करता है जिसे मैं ढूंढ रहा हूं।
जब मैं इस treeviewItem में नेस्टेड उल बिना चला यह मेरे पहले तीन आइटम ठीक कर देता है। जब मैं उल को जोड़ता हूं और नियंत्रण के लिए बाल वस्तुओं के साथ नियंत्रण प्राप्त करता हूं तो यह पृष्ठ को हाथ देता है और काम करना बंद कर देता है। के बारे में
मैं कैसे जाना चाहिए: काम नहीं कर रहा -
नेस्टेड उल साथ JSFiddle: -
नेस्टेड उल बिना JSFiddle काम कर रहे हैं (और आपके ब्राउज़र लटका सकता है!) एक नियंत्रण एक कस्टम निर्देश और ngRepeat प्रत्यावर्तन के संभावित अनंत स्तरों बनाने के लिए उपयोग करता है बनाने? मेरा दृष्टिकोण क्यों काम नहीं कर रहा है?
मैं $ में आए treeviews के अन्य उदाहरण को देखकर संकलन और उस विकल्प को मैं ले लिया था। यह खूबसूरती से काम करता है। यह कि आप मेरी मूल दृष्टिकोण क्यों काम नहीं करते हैं, इस बारे में स्पष्टीकरण के लिए, अब यह स्पष्ट है! ब्याज से, आप 'item.childitems' क्यों देख रहे हैं? क्या वह प्रत्येक बच्चे के सामान के लिए उल नहीं जोड़ देगा? – Jon
@Jon मैं अपने मूल जवाब है, यह होगा सरल संलग्न एक और 'ul' जब भी' childitems' परिवर्तन में इस तरह '$ watch' उपयोग नहीं करना चाहिए। मैंने '$ watch' का उपयोग किए बिना अपना जवाब अपडेट किया। इसके अलावा, क्या आप अपने प्रश्न में पाए गए वृक्षदृश्य उदाहरण को साझा कर सकते हैं ताकि हर कोई आपके प्रश्न से अधिक लाभ उठा सके? –
यह नहीं रह गया है कोणीय के नए संस्करण में काम करता है (> 1.2) –