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?

tonejac
  • 1,083
  • 3
  • 18
  • 32
  • 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 – argentum47 Apr 01 '15 at 08:02

4 Answers4

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) {
  ...
}]);
rhinosforhire
  • 1,305
  • 11
  • 20
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;
    });
};
        }
    ]);
squiroid
  • 13,809
  • 6
  • 47
  • 67
  • 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! – tonejac Apr 01 '15 at 14:30
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.

Hornth
  • 323
  • 5
  • 13
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>
yasin
  • 441
  • 4
  • 3