最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (angularjs) getting 'ReferenceError: $scope is not defined' when injecting controller - Stack Overf

programmeradmin0浏览0评论

I have am trying to learn AngularJS. I have a HTML page where I am trying to inject module but getting an error

The main js file:

var classificationModule = angular.module('mainapp',[]);

classificationModule.controller('firstcontroll',function(){
    $scope.text="Goodday"
});

The HTML PAGE:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="layout" content="main"/>
    <title>Classification Toolkit for Grails</title>          

    <script src=".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.

I have am trying to learn AngularJS. I have a HTML page where I am trying to inject module but getting an error

The main js file:

var classificationModule = angular.module('mainapp',[]);

classificationModule.controller('firstcontroll',function(){
    $scope.text="Goodday"
});

The HTML PAGE:

<!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./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.

Share Improve this question edited Sep 11, 2014 at 14:10 Nitsan Baleli 5,4583 gold badges34 silver badges53 bronze badges asked Sep 11, 2014 at 13:51 krskrs 1782 gold badges3 silver badges11 bronze badges 2
  • Replace @scope with $scope. @ is not a valid javascript token – Tiborg Commented Sep 11, 2014 at 13:52
  • Did That. A new error occurs. Does there seem to be a problem with the way I am laoding angular JS ? – krs Commented Sep 11, 2014 at 13:56
Add a ment  | 

2 Answers 2

Reset to default 3

Try passing $scope in the function.

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./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>

You should write

var classificationModule = angular.module('mainapp', []);

classificationModule.controller('firstcontroll', function($scope) {
  $scope.text = "Goodday"
});
<script src="https://ajax.googleapis./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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论