angularjs controller in directive

app.directive('customButton', function() {
  return {
    restrict: 'A',
    scope: true,
    template: '<button ng-click="clickMe()" type="button">Click Me</button>',
    replace: true,
    controller: function($scope, $element) {
      $scope.clickMe = function() {
         alert("yes Its working"); 
      }
    }
  }
});

Use this directive in html page like:

<span custom-button></span>

So above span will be replaced by html value used in template attribute of directive. And you will see a button labeled "Click Me".

When  you clicked on that button you should see a alert.


No comments:

Post a Comment