I have a controller and a directive. In the link function of the directive I invoke a controller function. When the controller function is invoked I want to access the scope of the controller inside the controller function. But I get $scope as undefined. How can I access the current scope. Here is the fiddle http://jsfiddle.net/9t294ox6/1/
I can access the scope using "this" keyword but $scope is undefined. Why does this happen?
This is my directive
app.directive('myDirective', function() {
return {
scope: { someCtrlFn: '&callbackFn' },
link: function(scope, element, attrs) {
scope.someCtrlFn({arg1: 22});
},
}
});
my controller
function MyCtrl($scope) {
$scope.ctrlFn = function(test) {
//Want to access scope here
debugger
console.log(test);
}
}
My HTML
<div ng-controller="MyCtrl">
<div my-directive callback-fn="ctrlFn(arg1)"></div>
</div>
console.log($scope);
and you'll see it's working fine – Maximus 36 mins ago