Since you have an isolated scope, the function belongs to the scope of the directive and not to your controller. Try using & in the scope of your directives as follows:
<body ng-controller="MainCtrl"> <div user-name="" callme="enableEditor()"></div> <div> <a href="#" ng-click="enableEditor()">add</a> </div> <script> var myApp = angular.module('myApp', []); myApp.controller('MainCtrl', ['$scope','$filter', function ($scope,$filter) { $scope.enableEditor = function() { alert("123"); }; }]); myApp.directive("userName", function() { return { restrict: "A", scope: { value: "=userName", callme:"&" }, template: '<div class="click-to-edit">' + '<a href="#" ng-click="callme()">Edit</a>' + '</div>' }; });
The callme="enableEditor()" attribute is used to pass the method to the scope directive; in the scope of the directive, & used to indicate that this is the callme:"&" method. Another example:
method2="someMethod()" as
scope: { value: "=userName", callme:"&", method2:"&" },template: '<div class="click-to-edit">' + '<a href="#" ng-click="callme()">Edit</a>' + '<a href="#" ng-click="Method2()">Save</a>' + '</div>'
Dalorzo
source share