1

I have the following directive. When I trigger the open function and get to the debugger I get an error message in the console that says Uncaught ReferenceError: $scope is not defined(…).

How is it possible for $scope.open to be called when $scope is undefined?

app.directive('photo', ['$http', 'modal', function($http, modal) {
    return {
        replace: true,
        templateUrl: '/assets/photo.html',
        transclude: false,
        scope: {
            result: '=',
            index: '@'
        },
        controller: ['$scope', '$http', 'modal', function($scope, $http, modal) {
            $scope.prev = $scope.index - 1;
            $scope.open = function() {
                debugger;
            };
        }]
    }
}]);

Here is my DOM:

<div ng-repeat="r in results" photo result="r" index="$index"></div>

If I insert console.log($scope) just before my open function, and then again right before the debugger in that function, I get the following results. Left is before open is called, right is after open is called. enter image description here

3
  • What do you see when you add this line before debugger; console.log($scope);. This may be important because the compiler may be optimizing your $scope variable away when you're inside of $scope.open. Commented Feb 8, 2016 at 19:06
  • I just updated my question with a screenshot. Note that temp1 != temp2. Commented Feb 8, 2016 at 19:11
  • $id shouldn't be a string. The AngularJS framework uses numbers for scope $id. Something outside Angular is messing with that reserved variable. Commented Feb 8, 2016 at 21:05

5 Answers 5

3

You inject the $http and modal in the directive definition (as you did), no need to in the controller function, just do:

controller: function($scope) {
        $scope.prev = $scope.index - 1;
        $scope.open = function() {
            debugger;
        };
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, so this solves the problem, but what I'd like to know is why? I have used the same dependency injection format for many other directives without any issues. Why would this one be different?
I'm sorry, but I don't really know the reason for that :(
His syntax to inject $scope should work just fine. Check out "Creating Directives that Communicate" in the docs.
@StevenWexler - Thank you, I will look into that
I wonder if it's somehow affected by ng-repeat, but that strikes me as strange, since most of my directives are attached to repeaters just like the snippet in my question.
|
1

Try adding a statement that uses $scope in $scope.open. Chrome has probably optimized $scope away when you're in $scope.open because you're not using it.

$scope.open = function() {
  console.log($scope);
  debugger; //now you should see $scope.
};

Comments

1

its worked for me

var app = angular.module("moduleTest",[]);
app.directive("testDirective",function(){
    return {
        restrict: "A",
        scope: true,
        link: function(scope, element){
           //code
           //and $scope is scope
        }
    }   
 });

Comments

0

This should work:

app.directive('photo', ['$http', 'modal', function($http, modal) {
return {
    replace: true,
    templateUrl: '/assets/photo.html',
    transclude: false,
    scope: {
        result: '=',
        index: '@'
    },
    controller: function($scope, $http, modal) {
        $scope.prev = $scope.index - 1;
        $scope.open = function() {
            debugger;
        };
    }
}
}]);

Comments

0

You need to define the $Scope at the top i.e.:
app.directive('photo', ['$http', '$Scope','modal', function($http, $Scope, modal)

It will work fine now.

1 Comment

You don't inject $scope into a directive like that. My dependency injection is correct. I know because I have dozens of other directives following the same format that work great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.