我一直在AngularJS中为此控制器代码获取’$scope not defined’控制台错误:
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; }); };
在我的AngularJS MVC文件中,我应该在哪里查找$scope未正确定义的问题?
解决方法
将该代码放在控制器内: –
angular.module('articles').controller('ArticlesController',Articles){ $scope.authentication = Authentication; $scope.create = function() { // THROWS ERROR ON THIS INSTANCE OF $SCOPE var article = new Articles({ title: this.title,function(errorResponse) { $scope.error = errorResponse.data.message; }); }; } ]);