My "Hello World" Angular JS is not working - javascript

I have been following a tutorial to learn Angular js, and i am using Web Storm 9.0.1.
I have simply created a Html page and loaded the Angular.js file to the project.
this is my simple code:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
<script src="angular.min.js"></script>
<script type="text/javascript">
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Hello World ";
}
</script>
</body>
</html>
But all i get when i run the page is :{{helloMessage}} as a header 1 !
What should i do ? what i am missing here ?

You need:
<body ng-app>
This ngApp is needed to make understand AngularJS where it has to work
Here's your the working code:
http://plnkr.co/edit/lPOknrPD5dykwImL6Pb9?p=preview

You just haven't initialized the Angular application. All you need to do is modify your body tag a bit. Notice the ng-app attribute in the body tag below:
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Hello World ";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</body>

check Angular hello word in plunker
<html ng-app="plunker">
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
</body>
</html>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});

You always need to put the directive first. You can also do it in or any other html tag. The ng-app directive tells the browser 'hey, this is an Angular app, let's see the script'. After the ng-app it will recognize all Angular directives.
It's also better practice to put the angular script loading at the bottom of the html, after the body. It's advised to load it last for performance and screen loading reasoning.

Thanks all, Yes indeed the problem was with the missing ng-app directive.
Note: the tutorial i was following missed this point!
A working Code:
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css/default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</header>
<section>
</section>
<footer>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script type="text/javascript">
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Hello World";
}
</script>
</body>
</html>

Missing the ng-app directive:
<body ng-app="">...</body>

Try this code if you have not found any solution yet.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title> Hello World </title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
</head>
<body>
<div ng-app="HelloWorldApp">
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</div>
<script>
// INITIALIZE THE APP.
var hello = angular.module('HelloWorldApp', []);
// ADD THE CONTROLLER.
hello.controller(
'HelloWorldCtrl',
['$scope', function ($greet) {
$greet.helloMessage = 'Hello World!';
} ]
);
</script>
</body>
</html>
I have defined the "ng-app" directive inside the DIV element and later initialized it in the script. This way I can add multiple view and controller inside the DIV element.
For example. Add another DIV below the header tag.
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
<div ng-controller="greet">
<span> {{greetings}} </span>
</div>
Later add this code inside the script tag.
// ADD THE SECOND CONTROLLER.
hello.controller(
'greet',
['$scope', function ($greet1) {
$greet1.greetings = 'Good Morning';
} ]
);

All you need to do is to define an AngularJS application on your page. The ng-app directive defines an AngularJS application. You can do it on your page like :
<html ng-app=""> or
<body ng-app=""> or
<div ng-app="">
Make sure you do it before you start writing other angular JS code.
For more on the fundamentals of Angular JS :
http://www.w3schools.com/angular/angular_intro.asp

change you angular version to 1.2, I did it and it worked.
here it is:
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js

Related

Not able to get Angular to work (even on git pages)

I am trying my first experiences with Angular, but I only get curly-braces for the angular expressions, even though the localhost server is running - it is the same on git pages..
Can anyone help me? Researching brings me more complex solutions such as stopping angluar displaying these braces on a more complex level, but I feel my question is very basic - but copying and testing as much as possible, I can't get it to work.
Below is the HTML:
<!DOCTYPE html>
<html ng-app="myFirstApp">
<head>
<meta charset="utf-8">
<script src="scripts/angular.min.js"></script>
<script src="scripts/app.js"></script>
<title>Hello Coursera</title>
</head>
<body>
<h1>Hello Coursera</h1>
<div ng-controller="MyFirstController">
{{$scope}}
</div>
</body>
</html>
The app.js in the same folder:
(function () {
'use strict';
angular.module('myFirstApp', [])
.controller('MyFirstController', funtion($scope) {
$scope.name = "simon";
});
})();
The angular.min.js file is located in the same folder and is downloaded directly.
Is this perhaps an old version or so?
Would really appreciate your help here.
Many thanks and best regards,
Simon
There are couple of mistakes in your code. First of all you dont write {{$scope}} in html . You directly write variable name like {{name}} then the function you spelled was wrong.
PFB the code and it will work for you.
(function () {
'use strict';
angular.module('myFirstApp', [])
.controller('MyFirstController', function($scope) {
$scope.name = "simon";
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="myFirstApp">
<head>
<meta charset="utf-8">
<script src="scripts/angular.min.js"></script>
<script src="scripts/app.js"></script>
<title>Hello Coursera</title>
</head>
<body >
<h1>Hello Coursera</h1>
<div ng-controller="MyFirstController">
{{name}}
</div>
</body>
</html>

ng-init not working on initializing

I have just started to learn angular.js but ng-init is not working in my code. I searched through internet but wasn't able to find any answer.
Here is my code thanks in advance !!
<!DOCTYPE html>
<html lang="en">
<head>
<title>ngClassifieds</title>
</head>
<body ng-app = "ngClassifieds" ng-init="message='hello, World!'">
<h1 ng-model="message">{{ message }}</h1>
<script src="node_modules/angular/angular.js"></script>
<script src="scripts/app.js"></script>
</body>
</html>
Add the code below in your app.js
var ngClassifieds=angular.module("ngClassifieds",[]);
I think the problem is with script loading
<head>
<title>ngClassifieds</title>
<script src="node_modules/angular/angular.js"></script>
<script src="scripts/app.js"></script>
</head>
add your scripts in header tag
Html Code
<body ng-controller="DemoCtrl" ng-app="main" ng-init="data='welcome'">
{{data}}
</body>
Script Code
var app = angular.module('main', []);
app.controller('DemoCtrl', function ($scope) {
});

ng-include doesn't work when ng-app gets a name

I'm using ng-include to include a navbar. In combination with ng-app="" it works fine.But when I use a name (e.g. ng-app="myApp") then it doesn't work anymore. See example.
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body >
<div ng-app="">
<div ng-include="'pages/navbar.html'"></div>
<script src="app.js"</script>
</div>
</body>
</html>
app.js
var app = angular.module('myApp', []);
app.controller('mainController', function($scope) {
});
This above works fine, but when I insert the name "myApp" it stops including the navbar.
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body >
<div ng-app="myApp">
<div ng-include="'pages/navbar.html'"></div>
<script src="app.js"</script>
</div>
</body>
</html>
Why?
You have a typo here
<script src="app.js"</script>
It should be
<script src="app.js"></script>
As long as this loads after angular.js then your application should be able to find the declaration of the myApp module (which you've asked it to by using ng-app="myApp"
Check in the browser console for any errors - if the script isn't loaded you will see an error like cannot find module
I believe you have missed including your app.js file in a proper style.
are you getting any error in console?
Edited
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body >
<div ng-app="myApp">
<div ng-include="'pages/navbar.html'"></div>
</div>
<script src="app.js"></script>
</body>
</html>

Angular ng-bind-html issue with emoji plugin?

I am using an angular emoji plugin that requires ng-bind-html. I am using this emoji plugin for creating messages, however I don't want it so that html code such as <h2>hello</h2> can be compiled but the ng-bind-html is mandatory to use the emoji plugin. Any suggestions/ways I can get around this so that posts will include emojis but not html code that gets compiled? Thanks
angular.module("app", ["dbaq.emoji","ngSanitize"]).controller("AppCtrl", function ($scope) {
$scope.message = "Animals: :dog: :cat: :snake: People: :smile: :confused: :angry: Places: :house: :school: :hotel: :poop:";
});
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="emoji.min.css">
<script src="angular.min.js"></script>
<script src="emoji.min.js"></script>
</head>
<body ng-app="app" ng-controller="AppCtrl">
<div ng-bind-html="message | emoji"></div>
</body>
</html>
emoji plugin --> https://github.com/dbaq/angular-emoji-filter-hd
Try my simple solution:
1. Create file html like this:
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="emoji.min.css">
<script src="angular.js"></script>
<script src="angular-sanitize.js"></script>
<script src="emoji.min.js"></script><script src="script.js"></script>
</head>
<body ng-app="app" ng-controller="AppCtrl">
<textarea ng-model="message"></textarea><div ng-bind-html="message | emoji"></div>
</body>
Notes:
I embed file js called angular-sanitize.js. Copy here and save as angular-sanitize.js. Then other files 'emoji.min.css, angular.js & emoji.min.js' exists on your app. Please mind your path too.
2.Create js file called script.js like this:
angular.module("app", ["emoji","ngSanitize"]).controller("AppCtrl", ['$scope', function ($scope) {
$scope.message = "Animals: :dog: :cat: :snake: People: :smile: :confused: :angry: Places: :house: :school: :hotel: :poop:";}]);
3.Run it.

script not running in templateurl

This is my angular js file
test.js file:
var app = angular.module("angleapp", [])
.controller('MainController', ['$scope', function ($scope) {
$scope.test = "hi all"
}])
.directive('programlisting', function () {
return {
restrict: 'E',
scope: {
},
templateUrl: '/directives/test.html'
};
});
test.html file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
alert("stop");
</script>
</head>
<body>
hi how are you
</body>
</html>
This is my index file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script src="/directives/test.js"></script>
</head>
<body data-ng-app="angleapp">
<div class="main" ng-controller="MainController">
<programlisting></programlisting>
</div>
</body>
</html>
Now when I try to run this
hi how are you
is coming in the output. but the alert window does not pop-op where i am i going wrong?
The problem is that script tags are not executed by browser automatically when injected with innerHTML (what happens in case of templateUrl). Normally this tags are evaluated programmatically (eval). jQuery does it but jqLite (internal jQuery-like implementation of Angular) does not.
In your case the fix is simple, move jquery script tag before angular, then Angular will use jQuery for DOM manipulations:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
Also, you need to clean up your test.html template, leave only body content. This is how test.html should look:
hi how are you
<script type="text/javascript">
alert("stop");
</script>
And finally, I don't know why you need to have script tag inside of partial template, but in most cases this is bad idea, and there are better approaches.
In partials html page(test.html), No need to write doctype, head and body tags. Just write your html elements and script. It should work.

Categories

Resources