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>
share|improve this question
    
you are already getting the value ; whatelse you need? – pro.mean 39 mins ago
    
I want to access the keyword $scope. $scope comes as undefined in my ctrlFn. I get the scope using keyword this. But $scope is undefined. Why does this happen – ASR 37 mins ago
    
put console.log($scope); and you'll see it's working fine – Maximus 36 mins ago
    
This is really weird. Try removing the console.log($scope) and stop the debugger inside ctrlFn and check the value of $scope on the console. It comes as undefined. But when I add the statement console.log($scope) $scope gets printed. – ASR 26 mins ago
1  
@ASR, see this answer – Maximus 9 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.