0

I am facing a problem with ng-click event. Below is my code. When I click on the element, it retrieves the span tag and if I call currentTarget, it gives the li element. I want to get the <a> element on ng-click.And after getting element i have to check whether it has a class named 'havesub', How can I achieve it?

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

Below is my function in controller

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

1 Answers1

0

var app = angular.module("ngClickApp", []);
app.controller('ngClickCtrl', ['$scope', function ($scope) {
$scope.click=function(event){
 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>
 <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 works fine for me

<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>
phani indra
  • 243
  • 1
  • 10