2017-05-14 88 views
0

我有一個指令,像這樣:AngularJS指令事件監聽器未擊發

<div ng-repeat="(key, value) in $parent.item.detailedPlan track by $index"> 
    <ul contenteditable="true" class="plan-actions" data-placeholder="Add Plan ..."> 
    <li ng-repeat="planItem in value" ng-show="(key+1)==$parent.dayItem.value"> 
     {{planItem}} 
    </li> 
    </ul> 
</div> 

和一個指令功能,如下所示:

.directive('planText', function(){ 
     return { 
     restrict:'E', 
     templateUrl: 'app/partials/planText.html', 
     link: function(scope, element, attrs){ 

      element.find("ul").bind('keydown', function(event){ 
      console.log("keydown event"); 
      if (event.keyCode == 8) { 
      // console.log("backspace pressed"); 
      if ($(this).children().length == 1 && $(this).children("li").text().split("").length == 0) { 
       console.log("stop li removal"); 
       event.preventDefault(); 
      } 
      } 
     }); 
     element.find("ul").bind('focus', function(event){ 
      if ($(this).children().length ==0) { 
       console.log("insert li"); 
       $(this).append("<li></li>"); 
      } 
     }); 
     } 
     } 
}) 

的數據結構從讀取,是數組的數組。 但是,這兩個事件偵聽器函數都不會觸發。添加一個$編譯也沒有幫助。請幫忙 !

+0

你可以請發佈演示?小提琴/ plunkr。你檢查過'element.find(「ul」)'返回了什麼嗎? –

+0

有點難以添加小提琴。但在鏈接階段,它確實找到了ul。但是處理程序不會觸發 –

+0

你是否用斷點輸入'if($(this).children()。length == 0)'? –

回答