Xabi Xabi - 1 month ago 10
Javascript Question

AngularJS get ng-click source element

I'm making a javascript calendar and I'd like to be able to call ng-click when I click on a day. Currently, I have that ng-click function working but I can't attach the clicked element to it:

var dateDiv = document.createElement('div');
dateDiv.setAttribute('ng-click', 'getTimeTables(' + fullDate + ', this);');
dateDiv.className += ' today';
$compile(dateDiv)($scope);


Also tried replacing this with angular's $event, and I can't get it working or I don't know how, as $($event.currentTarget).addClass('newclass'); doesn't seem to do anything.

Answer

You can access the element through $event.target

var dateDiv = document.createElement('div');
dateDiv.setAttribute('ng-click', 'getTimeTables(' + fullDate + ', $event);');
dateDiv.className += ' today';
$compile(dateDiv)($scope);

And inside getTimeTables:

$scope.getTimeTables = function(fullDate, $event) {
  $($event.target).addClass('newclass');
}
Comments