AngularJS:'$范围未定义'
AngularJS: '$scope is not defined'
我在AngularJS:中不断收到此控制器代码的"$scope未定义"控制台错误
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
周围的引号,则会出现此错误。
错误
app.controller('myCtrl', [$scope, function($scope) {
...
}]);
Happy Angular
app.controller('myCtrl', ['$scope', function($scope) {
...
}]);
将代码放入控制器中:-
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;
});
};
}
]);
只需将$scope.create函数放入控制器中即可。不要在外面!
$scope仅在控制器中定义,每个控制器都有自己的控制器。所以在控制器之外写入$scope是不起作用的。
检查在定义控制器之后声明的范围变量。例如:
var app = angular.module('myApp','');
app.controller('customersCtrl', function($scope, $http) {
//define scope variable here.
});
检查视图页面中控制器的定义范围。
例如:
<div ng-controller="mycontroller">
//scope variable used inside these blocks
<div>
相关文章:
- AngularJS:'$范围未定义'
- Javascript 变量范围未定义
- 从html调用angularjs控制器中的一个函数,但未定义范围变量
- AngularJS范围未定义
- 变量未定义/超出范围
- 因果报应测试中未定义的预期范围变量
- 在ColdFusion中表单范围中未定义AJAX请求的发布参数
- 创建可在任何地方使用的 JS 函数?范围和功能“未定义”的问题
- JavaScript 中全局范围内存在未定义变量的原因是什么?
- Angularjs 指令 - 未定义隔离的范围值
- 范围变量在函数内部未定义 - AngularJS
- 与“未定义”进行比较是否仍会进行全局范围查找
- AngularJS:控制器中未定义的范围变量
- Angular js 指令控制器访问范围属性,但返回未定义
- Javascript 范围:为什么 foo 返回未定义
- 错误业力“未定义”范围变量在之前每个
- 范围变量未定义在 angularjs 打字稿控制器类指令
- JavaScript 访问 Angular 范围返回“未定义” - 异步问题
- 范围问题/“未定义不是一个函数”
- 未定义范围