2013-07-10 42 views
0

我使用AngularJS中的指令成功加載我的樹。現在我想在我的樹中添加事件(這裏選擇節點),所以我喜歡這樣。但我看不到我的警報。如何在AngularJS中使用jsTree事件

我的代碼:

app.directive('jstree', function() { 
    return { 
    restrict: 'A', 
    scope: { 
      jstree: '=' 
     }, 

link: function(scope, element, attrs) 
     {   
     scope.$watch('jstree', function() 
     { 

      $(element).jstree({ 

          "json_data" :{ 
          "data":scope.jstree.data 
          }, 

          "themes" : { 
            "theme" : "classic", 
             "dots" : true, 
            "icons" : true 
            }, 
          "plugins" : [ "themes", "json_data" ] 
         }, false); 
      }, true); 

      // select a node 
      element.bind("select_node.jstree",function(e, data) { 
    $window.alert(e.data); 

         }); 

      } 
      }; 
}); 

任何想法是我在哪裏呢?

+0

您可以嘗試在'tree'等變量中存儲'$(element).jstree({...})'的引用,然後執行'tree.bind' – Chandermani

+0

@Chandermani,我試試看,但我仍然沒有結果。 –

回答

1

使用事件jstree,你必須在這一行中添加 「用戶界面」:

「插件」: 「主題」, 「json_data」, 「用戶界面」。

現在,它的工作。

0

看着jstree演示,你會想要在jstree對象上調用綁定,而不是在元素上(你可以綁定到元素上的click,但這可能不是你想要的)

$(element) 
    .jstree({ 
    "json_data" : { 
     "data" : scope.jstree.data 
    }, 
    "themes" : { 
     "theme" : "classic", 
     "dots" : true, 
     "icons" : true 
    }, 
    "plugins" : ["themes", "json_data"] 
    }, false) 
    .bind('select_node.jstree', function(ev,data) { 
    console.log('clicked'); 
    }); 
+0

嗨@leon,我做到了,沒有結果,在我的控制檯中,我看不到被單擊的單詞。 THKS –