AngularJS: '$ scope not defined'

I keep getting "$ scope is not defined" 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 look so that problems with are not identified correctly?

+4
source share
4 answers

Put this code inside the 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;
    });
};
        }
    ]);
+9
source

For others who land here from Google, you will get this error if you forget the quotes around $scopewhen you annotate a function to minimize it.

Error

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

Happy angular

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

$scope.create . !

$scope , . $scope .

+3

Check the scope variable declared after the controller definition. For instance:

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

});

Check the specific range of the controller on the watch page.

For instance:

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

<div>
0
source

All Articles