6

I keep getting '$scope is not defined' console errors for this controller code in AngularJS:

angular.module('articles').controller('ArticlesController', ['$scope', '$routeParams', '$location', 'Authentication', 'Articles',
        function($scope, $routeParams, $location, Authentication, Articles){
            $scope.authentication = Authentication;
        }
    ]);


$scope.create = function() { // THROWS ERROR ON THIS INSTANCE OF $SCOPE
    var article = new Articles({
        title: this.title,
        content: this.content
    });

    article.$save(function(response) {
        $location.path('articles/' + response._id);
    }, function(errorResponse) {
        $scope.error = errorResponse.data.message;
    });
};

Where in my AngularJS MVC files should I be looking at to find problems with the $scope not being defined properly?

1
  • yeah, that's correct error. $scope is injected into your controller, or anyfactor or service, its not available globally. you has to put it in the controoler or factory.... Angular parses the anonymous function's args as string and then assigns the value if defined. approximately Commented Apr 1, 2015 at 8:02

4 Answers 4

20

For others who land here from Google, you'll get this error if you forget the quotes around $scope when you're annotating the function for minification.

Error

app.controller('myCtrl', [$scope, function($scope) {
  ...
}]);

Happy Angular

app.controller('myCtrl', ['$scope', function($scope) {
  ...
}]);
Sign up to request clarification or add additional context in comments.

Comments

9

Place that code inside controller:-

angular.module('articles').controller('ArticlesController', ['$scope', '$routeParams', '$location', 'Authentication', 'Articles',
        function($scope, $routeParams, $location, Authentication, Articles){
            $scope.authentication = Authentication;

$scope.create = function() { // THROWS ERROR ON THIS INSTANCE OF $SCOPE
    var article = new Articles({
        title: this.title,
        content: this.content
    });

    article.$save(function(response) {
        $location.path('articles/' + response._id);
    }, function(errorResponse) {
        $scope.error = errorResponse.data.message;
    });
};
        }
    ]);

1 Comment

Ahhh.... I didn't realize I'd placed the '.create' method outside the controller function. (newbie to AngularJS) Thanks for the guidance and pointing out the problem!
4

Just put you $scope.create function inside your controller. Not outside !

$scope is only defined in controllers, each controller have its own. So write $scope outside your controller can't work.

Comments

0

Check scope variable declared after controller defined. Eg:

    var app = angular.module('myApp','');
     app.controller('customersCtrl', function($scope, $http) {
     //define scope variable here.

});

Check defined range of controller in view page.

Eg:

<div ng-controller="mycontroller">
//scope variable used inside these blocks

<div>

Comments

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.