3

I'm trying to learn Angular.JS, so I wrote this simple code just to test it:

<!doctype html>
<html ng-app="myapp">
<head>
<meta charset="utf-8">
<title>Angular.JS</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
</head>

<body>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript" src="js/myapp.js"></script>
    <p>The sum of 5+10 is: {{5 + 10}}</p>
</body>
</html>

In the browser I'm supposed to see the following output: The sum of 5+10 is: 15, but I just see it as it is: The sum of 5+10 is: {{5 + 10}}. I tried it both in Chrome and FF, I tried to add the angular.js from Google CDN with <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>, I tried to move the script between the <head> tags, but the outcome is always the same.

What am I doing wrong? Why don't I have the output correctly?

7
  • @IgorSemin It's currently an empty file, no code there. Commented Nov 3, 2014 at 14:06
  • <html ng-app> or look your code in myapp.js Commented Nov 3, 2014 at 14:06
  • 1
    I suggest you change the title of your question. Your question has nothing to do with whether Angular.js works. It has to do with you not understanding how Angular works. Commented Nov 3, 2014 at 14:06
  • @Igal then remove ng-app name, just <html ng-app> because angular wait initialization module. Or you can add to your myapp.js below code var app = angular.module("myapp", []); Commented Nov 3, 2014 at 14:10
  • Did you initialize the app in myapp.js? var app = angular.module('myapp', []); Commented Nov 3, 2014 at 14:11

4 Answers 4

4
<html ng-app="myapp">

When you specify a name for ng-app, it's expecting to find a user-created module with that name. If you're truly just wanting your example to work, you can simply remove the ="myapp" and you'll be all set.

<html ng-app>

If you want to keep your "myapp" name, add this script block after you load angular.

<script type="text/javascript">
  angular.module('myapp', []);
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Either remove name initialization in html:

<html ng-app>

Or initialize module in your javascript file:

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

Second way is better.

2 Comments

This answer is far from being helpful. It 's vague and lacks detail.
@Romario the all details already there are in the start question as my opinion. 1 html file + 1 js file, I think my answer is quite enough for that. It is Angular 1st version, and I don't think that it is an actual answer for the new versions of angular.
0

Just use "ng-app":

<html>
 <head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
 </head>
 <body ng-app>
  {{ 5 + 10 }}
 </body>
</html>

Fiddle here.

Comments

0

This should work.

You need to instanciate the module myapp you are using here

<html ng-app="myapp">

with the following:

<script type="text/javascript">
    var app = angular.module('myapp', []);
</script>

The final html is like this:

<html ng-app="myapp">
<head>
<meta charset="utf-8">
<title>Angular.JS</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script type="text/javascript">
  var app = angular.module('myapp', []);
</script>

</head>

<body>
    <script type="text/javascript" src="js/myapp.js"></script>
    <p>The sum of 5+10 is: {{5 + 10}}</p>
</body>
</html>

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.