【发布时间】:2015-04-01 07:59:16
【问题描述】:
我不断收到 AngularJS 中此控制器代码的“$scope is 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 定义不正确的问题?
【问题讨论】:
-
是的,这是正确的错误。 $scope 是
injected到您的控制器或任何因素或服务中,它在全球范围内不可用。你必须把它放在控制器或工厂中...... Angular 将匿名函数的 args 解析为字符串,然后分配值(如果已定义)。大约
标签: javascript angularjs mean-stack