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>
Related
I have created a basic angular app, with self invoking function
(function(){})
When ever I am running the application it is throwing injector error, if I remove the above code, it is working fine, I have gone through many sites but unable to fix this, and why it is happening I am unable to understand.
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="myApp">
<h1>Hello Plunker!</h1>
</body>
</html>
script.js:
(function(){
angular.module("myApp", [])
});
Demo
You need to have like this,
(function(){
angular.module("myApp", [])
})();
DEMO
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) {
});
I am trying to code for the first time ever in Angular, and I can't load an expression on my page because I get the error [$injector:modulerr] Failed to instantiate module. I know there are a lot of similar issues on stackoverflow but none of them have solved my issue. The link to app.js is not broken.
HTML
<!DOCTYPE html>
<html ng-app="store">
<head></head>
<body>
<script type"text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.js"></script>
<script type="text/javscript" src="js/app.js"></script>
<p>{{ "hello" + "there"}}</p>
</body>
</html>
app.js
(function(){
var app = angular.module('store', [ ]);
})();
I am just trying this locally on my computer. Thanks!
Your only problem are the typos in the code.
There's an = missing in the first script (line 5) and it's written javscript instead of javascript in the second script (line 6)
Try the following code
<!DOCTYPE html>
<html ng-app="store">
<head>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.js"></script>
<script type="text/javascript">
(function() {
var app = angular.module('store', []);
})();
</script>
<p>{{ "hello" + "there"}}</p>
</body>
</html>
Hope it helps!
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
I'm trying to understand what's going on here. The warning is self explanatory, and I realize that in the app, with the code and structure I have below, it runs the ng-view twice ('test' will be logged twice in the console, so of course angular is loaded twice!)....but why?
I've read every post I could find about it, and it seems to boil down to jQuery being loaded before angular.
If I leave out jQuery or if I load jQuery after angualr (which isn't good practice from what I understand), no problem. I'd like to have jQuery to support some functionality (specifically ui-sortable). And, although it doesn't seem to be actually causing any problems, I'd like to not have it running my ng-view twice.
Am I doing something structurally wrong, or am I missing an obvious way to fix this?
Update: Plunker of the issue (check the console)
index.html:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Site Title</title>
</head>
<body ng-view>
<script type="text/javascript">
console.log('test')
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
<script type="text/javascript" src="app_simple.js"></script>
</body>
</html>
app_simple.js:
'use strict';
/**
* Configure client module
*/
var myApp = angular.module('myApp',
[
'ngRoute'
]);
myApp.config(function ($routeProvider) {
$routeProvider
.when('/simple', {
templateUrl: 'components/simple/simple.html',
controller: 'SimpleCtrl'
})
.otherwise({
redirectTo: '/x'
})
});
myApp.controller('SimpleCtrl', ['$scope', '$log', '$http', function($scope, $log, $http){
}]);
simple.html:
My simple content
Ok, to summarize the help from the comments:
If jQuery is included, any <script> tags included WITHIN an ng-view will be evaluated twice. (Thanks #lossleader!).
My incorrect assumption in testing was that it was processing the entire template content twice when I tried moving the ng-view off the body onto a <div> because I saw the log message twice. It wasn't!
<body>
<div ng-view>
<script>console.log('duplicated if jQuery');</script>
</div>
</body>
So #Tom and #Wawy both had correct solutions. Either move the ng-view into a <div> or move the <script> tags into the <head> (outside of the ng-view).
i have same issue, and i fix it by loading JQuery before i load AngularJS
hope that works for you