如何用jqlite以角度抓取元素的attr数据?

问题描述:

如何以jqlite的角度抓取元素的attr数据?如何用jqlite以角度抓取元素的attr数据?

HTML,

<button ng-click="loadForm($event)" href="form.php">Load Directive Form</button> 
<div data-my-form></div> 

角,

 app.directive('myForm', function() { 
     return { 
      controller:function($scope){ 
      $scope.isLoaded = false; 
      $scope.loadForm = function(e){ 

       console.log(e.target.attr('href')); // error 

       var form = e.target.attr('href'); // error 

       $scope.isLoaded = true; 
      } 
      }, 
      template: '<div><div ng-if="isLoaded" ng-include="'+ form +'" ></div></div>', 
      link:function(scope, element) { 

      } 
     } 
    }); 

我得到这个错误,

TypeError: e.target.attr is not a function 

什么想法?

+0

使用'e.target.href'或'e.target.getAttribute( 'href' 属性)',这取决于是否你想要绝对或相对的链接。我仍然想知道为什么将_href_属性分配给非链接元素。 – raina77ow 2014-12-13 09:10:21

+0

我得到这个错误'错误:[$ parse:syntax]语法错误:在[HTMLDivElement]]开始的表达式[[object HTMLDivElement]]的第9列处出现[Token'HTMLDivElement'是意外的,期待[]]' – laukok 2014-12-13 09:13:25

+1

' form'变量定义为控制器函数的本地变量,但您尝试在模板中使用它。 – raina77ow 2014-12-13 09:16:03

attr()是可用于jqLit​​e元素的方法。

e.target是对DOM元素的引用。

您需要包装的DOM元素作为jqLit​​e元素第一:

var form = angular.element(e.target).attr('href'); 
+0

非常感谢你的答案。 – laukok 2014-12-13 10:14:48

+1

不客气:) – tasseKATT 2014-12-13 10:19:18