Keep getting Angular JS error not sure why - javascript

I am practicing Angularjs and made a simple module and controller. Im trying to display an elements data inside the controller on the HTML view page but the {{student.name}} only pops up. and im getting an error message saying:
angular.js:15697 Error: [$controller:ctrlreg] http://errors.angularjs.org/1.8.2/$controller/ctrlreg?p0=MyController
Here is my js code:
var myApp= angular.module('myApp',[]);
myApp.controller=('MyController', function($scope){
$scope.student={
"name":"kevin",
"age":"21",
"school":"NYU"
};
});
Here is my HTML code:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<!-- angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="app.js"></script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body ng-controller="MyController">
{{student.name}}
</body>
</html>
please help.

The way you have defined your controller is incorrect. You can do something like below:
angular.module('app').controller('MainCtrl', function ($scope){
$scope.student={
"name":"kevin",
"age":"21",
"school":"NYU"
};
});
Sample working code here:
https://stackblitz.com/edit/angularjs-controllers-eauszw?file=app.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>

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.

Error: [ng:areq]

I am getting error ng:areq with the following code.please help me to figure out where it is causing.Thanks!
<!DOCTYPE html>``
<html ng-app>
<head>
<title>This is example</title>
</head>
<body>
<h1 ng-controller="HelloWorldMessage">{{helloMessage}}</h1>
<script src="angular.min.js"></script>
<script type="text/javascript">
function HelloWorldMessage($scope){
$scope.helloMessage="Hello World";
}
</script>
</body>
</html>
notice that your controller is not defined you just defined a function ,, you need to create a module first
angular.module("app",[]);
and then add you controller to this module using this line
angular.module("app").controller("HelloWorldMessage", HelloWorldMessage);
this way your code will work properly

My "Hello World" Angular JS is not working

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

WARNING: Tried to load angular more than once...because of jQuery...why?

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

Categories

Resources