Angular: call controller function from directive (link)

http://angularjs.org/

by wwwroot

HTML

<div ng-controller="MyCtrl">
    <button confirm="Are you sure?" confirm-action="doIt()">Do It</button>
</div>

JavaScript

var app = angular.module('myApp', []);

app.directive('confirm', function ($parse) {
    return {
        restrict: 'A',
        scope: true,
        link: function (scope, element, attrs) {
            element.bind('click', function (e) {
                scope.$apply(attrs.confirmAction);
            });
        }
    };
});

function MyCtrl($scope) {
    $scope.doIt = function () {
        alert('hi')
    };
}