如何通过点击<a> in angular?</a>来获得href

时间:2014-01-30 09:44:04

标签: angularjs angularjs-directive

让我们说我们有一个

   <a href = "/jsondata" ng-click="myFunction()">Click Here</a>

在基于角度的应用程序中。

$scope.myFunction= function(){
        // how to get the href
        $http.get(href).success(function(data) {
        })
}

要求是单击并执行ajax请求并从服务器获取json数据。因为我选择处理角度,我不知道如何角度,因为我不知道如何从元素中获取href属性

2 个答案:

答案 0 :(得分:3)

尝试:

<a href = "/jsondata" ng-click="myFunction($event.target.href);$event.preventDefault()">Click Here</a>

JS:

$scope.myFunction= function(href){
        // how to get the href
        $http.get(href).success(function(data) {
        })
}

DEMO

答案 1 :(得分:1)

我使用angularjs 1.5和$ event.target.href在我的应用程序中无效。

我必须这样做:

<a ng-click="$event.preventDefault();vm.openToolTip($event);" href="J3606">position 36.06</a>

JS

function openToolTip(event){
    var href = event.currentTarget.attributes["href"].nodeValue;
    alert(href);
}