wendelflakes
wendelflakes

Reputation: 23

Detect function run on click

I'd like to track user activity in my angular app by creating a log of the functions tied to buttons the user clicks.

I figured the best way would be to create an event listener which logs the function name and any parameters associated with the clicked element.

I've been able to retrieve the element, but am unsure of how to get the function name and parameters.

Any help would be much appreciated.

Here's my code so far, so if I click a button which includes myFunction, i'd like the clickListener to log that 'myFunction' was called and 'foo' was the parameter passed in.

$scope.clickListener = function () {
    document.addEventListener("click", function (event) {
        console.log(event.target);
    }, false);
};

$scope.myFunction = function (foo) {
    //do stuff
}

<button ng-click="myFunction('foo')"></button>

Upvotes: 1

Views: 93

Answers (2)

Fareed Alnamrouti
Fareed Alnamrouti

Reputation: 32174

Why you don't just do:

var fun = event.target.getAtttibute("ng-click").split("(");
var name = fun[0];
var params = fun[1].split(",").pop();

Upvotes: 0

Charlie
Charlie

Reputation: 23818

You can get the name of the function by arguments.callee.name. Needless to say that arguments of the above construct give you the list of arguments passed.

$scope.clickListener = function () {
    document.addEventListener("click", function myListener(event) {
        console.log(event.target);
        console.log(arguments.callee.name)
    }, false);
};

Upvotes: 1

Related Questions