I have a controller like this:
angular.module('main', []).controller('mainCtrl', function ($scope) { $scope.doStuff = function (cb) { // Do some stuff. cb(); }; }); And I have a directive like this:
angular.module('action-bar', []).directive('actionBar', function () { return { restrict: 'EA', replace: true, template: '<h1>test</h1>', scope: { doStuff: '&' }, link: function (scope, element, attrs) { scope.doStuff(function () { alert('callback executed'); }); } }; }); Here is the markup:
<div ng-app="main"> <div ng-controller="mainCtrl"> <div ng-action-bar></div> </div> </div> I can call doStuff just fine, however the cb argument is always undefined even though I'm passing in an anonymous function as the callback. What am I doing wrong here?
doStuffattribute to youractionBardirective in your HTML? You didn't give us a sample of how you are using it. Just from a quick glance I think you need to add adoStuff="doStuff()"to your HTML.. or just create a jsfiddle so we can see it bettermainCtrl. Therefore thedoStufffunction is inherited into the isolate scope from the controller scope.attrs. I'm simply trying to pass an argument to the parent scope method from within the link function.