2014-02-18 34 views
1

我正在生成一个指令,通过包含将链接附加到DIV。现在我想链接被点击时执行一些功能,而不是去我已经包含了虚拟的href(在这种情况下google.com),例如...。由Angular指令生成的链接上的.stopPropagation()与包含

.directive('deleteDiv', function() { 
     return { 
      restrict: 'A', 
      transclude: true, 
      template: '<a href="http://www.google.com" ng-click="removeDiv()">X</a> ', 
      link: function (scope, elem) { 
       // now we don't want the link to go to the href! 
       scope.removeDiv = function(e){ 
        e.stopPropagation(); 
        console.log(elem); // but we do more things later.... 
       } 
      } 
     } 
    }) 

当我点击链接,我们对得到一个错误,因为e是未知的,href将我们发送到谷歌。即使我将return false;添加到removeDiv方法或更改e对于event我们仍然会发送到href位置。 (可能需要一个很好的解释)我如何使这项工作任何想法

回答

0

使用ng-click改为只HREF的,然后调用$event.stopPropagation();提到 here