javascript (angularjs) 在注入控制器时得到“ReferenceError: $scope is not defined”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25789243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(angularjs) getting 'ReferenceError: $scope is not defined' when injecting controller
提问by krs
I have am trying to learn AngularJS. I have a HTML page where I am trying to inject module but getting an error
我正在尝试学习 AngularJS。我有一个 HTML 页面,我试图在其中注入模块但出现错误
The main js file:
主要js文件:
var classificationModule = angular.module('mainapp',[]);
classificationModule.controller('firstcontroll',function(){
$scope.text="Goodday"
});
The HTMLPAGE:
该HTML页:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="layout" content="main"/>
<title>Classification Toolkit for Grails</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js"></script>
<asset:javascript src="main.js"/>
</head>
<body >
<div ng-app='mainapp' ng-controller='firstcontroll'>
Hello, {{text}}
</div>
</body>
</html>
Getting the following error :
收到以下错误:
ReferenceError: $scope is not defined
I dont know what I am doing wrong.
我不知道我做错了什么。
回答by Tony
Try passing $scope
in the function.
尝试传入$scope
函数。
var classificationModule = angular.module('mainapp', []);
classificationModule.controller('firstcontroll', function($scope) {
$scope.text = "Goodday"
});
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="layout" content="main" />
<title>Classification Toolkit for Grails</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js"></script>
<asset:javascript src="main.js" />
</head>
<body>
<div ng-app='mainapp' ng-controller='firstcontroll'>
Hello, {{text}}
</div>
</body>
</html>
回答by Tiborg
You should write
你应该写
var classificationModule = angular.module('mainapp', []);
classificationModule.controller('firstcontroll', function($scope) {
$scope.text = "Goodday"
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='mainapp' ng-controller='firstcontroll'>
Hello, {{text}}
</div>
$scope is a variable provided by angular JS' dependency injection system, and basically it works by matching providers to named parameters.
$scope 是 angular JS 的依赖注入系统提供的一个变量,基本上它通过将提供者与命名参数匹配来工作。