Fizzix Fizzix - 7 months ago 83
Javascript Question

Capturing a click event on all md-buttons with directive with Angular Material

I'm using Angular Material as a UI framework in my AngularJS web application.

I am trying to perform a specific action on each

md-button
click. I assumed that it would just be as simple as adding a directive, although unfortunately nothing happens.

app.directive('md-button', function(){
return {
restrict: 'AEC',
link: function(scope, element){
$(element).on('click', function (e) {
console.log("button clicked");
});
}
};
});


How can I perform an action on every
md-button
click on my site?

EXAMPLE

Answer

You have to declare your directive name using camelcase like this:

.directive('mdButton', function(){
  return {
    restrict: 'AEC',
    link: function(scope, element){
        angular.element(element).on('click', function (e) {
            console.log("button clicked");
        });
    }
  };
});
Comments