2016-01-18 20 views
0

我想使用angularjs編寫treeview。我使用ng-includerecursive調用..一切都很好,除了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' 
    } 
]; 
}); 


這裏是輸出畫面:

enter image description here
例如當選擇paridokht節點,爲了警示層級是paridokhtMacBook筆記本筆記本電腦電腦(從孩子的父母)。
請幫我解決這個問題。這太痛苦了! :(

回答

1

從嘗試阻止事件冒泡-ING在DOM樹起來

在你ng-click

ng-click='nodeSelected($event, category)' 

在你的控制器:

$scope.nodeSelected = function($event, category){ 
    $event.stopPropagation(); 
    alert('This node is selected' + category.title); 
} 
+0

沒有,當我使用這個,在每個'ng-click'中選擇的唯一節點是根父節點:( – Parid0kht

+0

我錯了,並且在第一次嘗試時犯了錯誤。)謝謝:) – Parid0kht