2017-07-07 53 views
0

我正面臨ng-click事件的問題。以下是我的代碼。當我點擊元素時,它會檢索span標記,如果我調用currentTarget,它會給出li元素。我想獲得ng-click上的<a>元素。並且在獲取元素之後,我必須檢查它是否有一個名爲'havesub'的類,我該如何實現它?要通過ng-click使用angularjs來獲取點擊元素的標記名

<li> 
    <a class="havesub" ui-sref="" ng-click="openmenu($event)"> 
    <img src="images/icon-03.png" alt="menu" /> 
    <span>Menu item1</span> 
    </a> 
</li> 

下面是我在控制器功能

app.controller("con", function($scope) { 
    $scope.openmenu = function($event) { 
    var targets = $event.currentTarget; 
    }; 
}); 

回答

0

var app = angular.module("ngClickApp", []); 
 
app.controller('ngClickCtrl', ['$scope', function ($scope) { 
 
$scope.click=function(event){ 
 
\t alert(angular.element(event.currentTarget).hasClass("havesub")); 
 
} 
 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<html ng-app="ngClickApp"> 
 
<head> 
 
\t <title>Validation messages</title> 
 
</head> 
 
<body ng-controller="ngClickCtrl"> 
 
<form name="userForm"> 
 

 
<li> 
 
    <a href class="havesub" ng-click="click($event)"> 
 
    <img src="1.png" alt="menu" /> 
 
    <span>Menu item1</span> 
 
    </a> 
 
</li> 
 

 
</form> 
 
</body> 
 
</html>

event.currentTarget工作正常,我

<html ng-app="ngClickApp"> 
<head> 
    <title>Validation messages</title> 
</head> 
<body ng-controller="ngClickCtrl"> 
<form name="userForm"> 

<li> 
    <a href class="havesub" ng-click="click($event)"> 
    <img src="1.png" alt="menu" /> 
    <span>Menu item1</span> 
    </a> 
</li> 

</form> 
<script type="text/javascript"> 
var app = angular.module("ngClickApp", []); 
app.controller('ngClickCtrl', ['$scope', function ($scope) { 
$scope.click=function(event){ 
    alert(angular.element(event.currentTarget).hasClass("havesub")); 
} 
}]); 
</script> 
</body> 
</html> 
相關問題