0

I've got a very basic page with a controller, I think it should be pretty obvious what I'm trying to do from the code, but it isn't working.

Here is my HTML

<!DOCTYPE html>
<html ng-app>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <h1>{{ 800 / 40 }} </h1>
    <h1 ng-controller="MainController">{{message}}</h1>
  </body>
</html>

With the following in script.js

var MainController = function($scope) {
     $scope.message = "Hello";
}); 

The 800 / 40 is displaying 20, so that suggests the Angular script and ng-app is fine, but the value of message isn't being set. In the example the ng-controller is in a h1 tag, but I have also tried it in a div which wraps around the h1.

Any suggestions?

Thanks

1
  • You should read the documentation instead of just defining something somehow. Commented Sep 15, 2016 at 9:20

5 Answers 5

2

You need to create an angular module

var myapp = angular.module('myApp', []);

myapp.controller('MainController', function($scope){
  $scope.message= "Hello";

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<div ng-app="myApp">
    <h1 ng-controller="MainController">{{message}}</h1>
</div>

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

Comments

2

You have to create a Angular module and place that module name for ng-app attribute.

Working solution: https://jsbin.com/nugipinale/1/edit?html,js,output

Comments

1

Define angular controller as

app.controller('MainController', function($scope){
$scope.message = "Hello";
});

2 Comments

Probably because app is undefined.
Where does your app comes from ?
1
   <div ng-app="myApp" ng-controller="MainController">
   <h1>{{ 800 / 40 }} </h1>
  <h1 >{{message}}</h1>
 </div>

<script>
var app = angular.module('myApp', []);
 app.controller('MainController', function($scope) {
  $scope.message = "John";
   });
 </script>

Comments

1

do something like this,

Html code

 <html>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
 <div ng-app="myApp" ng-controller="MainController">
   <h1>{{ 800 / 40 }} </h1>
  <h1 >{{message}}</h1>
 </div>
 </html>

add this in Script file

var app = angular.module('myApp', []);
 app.controller('MainController', function($scope) {
  $scope.message = "John";
   });
 </script>

3 Comments

Please try with new
@RajuDetroja No it doesn't: Uncaught Error: [$injector:nomod] Module 'myapp' is not available!. You have to use module('myapp', []) to define the module first.
Here , i changed and add full code (html and script code)

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.