0
我想使用angularjs
編寫treeview
。我使用ng-include
爲recursive
調用..一切都很好,除了ng-click
..當每個節點被點擊時..層次調用是從孩子到它的父母,併爲這個層次結構中的每個節點,火災ng-click
。我怎麼能解決這個問題?? ..我有這個確切的問題使用另一種方法(附加元素post-link
,我認爲這不是一個好方法),而不是ng-include
。
這裏是我的代碼:
angularjs Treeview使用ng-include爲所有節點的父母點擊ng-click
的index.html:
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-rc.0/angular.min.js"></script>
</head>
<body >
<div ng-app="app" ng-controller='AppCtrl'>
<ul>
<li ng-repeat="category in categories" ng-click='nodeSelected(category)' ng-include="'template.html'"></li>
</ul>
</div>
<script src="controller.js"></script>
</body>
</html>
template.html:
{{ category.title }}
<ul ng-if="category.categories">
<li ng-repeat="category in category.categories" ng-click='nodeSelected(category)' ng-include="'template.html'">{{ category.title }}</li>
</ul>
controller.js
var app = angular.module('app', []);
app.controller('AppCtrl', function ($scope) {
$scope.nodeSelected = function(category){
alert('This node is selected' + category.title);
}
$scope.categories = [
{
title: 'Computers',
categories: [
{
title: 'Laptops',
categories: [
{
title: 'Ultrabooks'
},
{
title: 'Macbooks',
categories:[
{
title:'Paridokht'
},
{
title:'Shahnaz',
categories:[
{
title:'Sohrab'
}
]
}
]
}
]
},
{
title: 'Desktops'
},
{
title: 'Tablets',
categories: [
{
title: 'Apple'
},
{
title: 'Android'
}
]
}
]
},
{
title: 'Printers'
}
];
});
這裏是輸出畫面:
例如當選擇paridokht節點,爲了警示層級是paridokht,MacBook筆記本,筆記本電腦,電腦(從孩子的父母)。
請幫我解決這個問題。這太痛苦了! :(
沒有,當我使用這個,在每個'ng-click'中選擇的唯一節點是根父節點:( – Parid0kht
我錯了,並且在第一次嘗試時犯了錯誤。)謝謝:) – Parid0kht