2

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

2
  • Replace @scope with $scope. @ is not a valid javascript token 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 ? Commented Sep 11, 2014 at 13:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.