user91579631 user91579631 - 26 days ago 5
AngularJS Question

Pass information from angular material button to controller

I am using angularjs v1 and angular material.

I am using this link as reference for adding angular material button.

https://material.angularjs.org/latest/demo/button

I have this simple angular material button. Here is the html code;

<md-button class="md-raised">Add Data</md-button>


When this button is pressed, I would like to run
console.log('button pressed');
inside my angular controller.

.controller('DataAddCtrl', ['$scope',
function ($scope) {

}])

Answer

Simple:

<md-button class="md-raised" ng-click="myFunc()">Add Data</md-button>

and in your controller:

.controller('DataAddCtrl', ['$scope', 
    function ($scope) {
      $scope.myFunc = function() {
         console.log('button pressed');
      }
    }])

Learn more about ng-click here: https://docs.angularjs.org/api/ng/directive/ngClick