2016-11-16 41 views
0

我在Angular中使用typescirpt。角度打字稿指令,在元素鏈接中查找div

我創建了一個指令,這只是一個div包含文本和一個類名「塊關閉」

我需要一個點擊功能添加到關閉按鈕關閉按鈕。

如何將點擊事件添加到指令中的按鈕。

angular.element('.block-close').on('click', function(){ 
     alert('here'); 
    }); 

    angular.element(document).find('.block-close').on('click', function(){ 
     alert('here'); 
    }); 

    (()=>{ 

     class MyDirective implements ng.IDirective{ 

      public restrict = 'E'; 
      public scope = {}; 
      public controller = 'MyController'; 
      public controllerAs = 'myCtrl'; 
      public bindToController = true; 
      public templateUrl = "mysite.html"; 

      constructor(){ 

      } 

      link = (scope, element, attrs) => { 
       angular.element('.block-close').on('click', function(){ 
        alert('here'); 
       }); 
      }; 

     } 

     angular.module('myMod').directive('myDirective',()=> new MyDirective()); 

    })(); 

回答

0

我試過的東西,我只是假設你的「mysite.html」有這樣的

<div> 
<input type="button" value="close" class="block-close"/> 
</div> 

代碼,並要處理單擊事件添加按鈕有類「塊關閉」。

實際上鍊接的功能在裏面可以訪問該指令的範圍,從而只顯示如下一個功能添加,

link = (scope, element, attrs) => { 
       scope.closeClicked =() =>{ 
          alert("clicked.."); 
        } 
      }; 

,並更新你的「mysite.html」像下面

<div> 
    <input type="button" value="close" ng-click="closeClicked()" class="block-close"/> 
</div>