AngularJS simple ng-controller error - javascript

I am learning angularJS, ng-app works ng-model works, but when I try ng-controller, it does not work, the fault is on my side but I am unable to figure out what it is, I am not even trying anything fancy, just trying make the ng-controller work,
what is my mistake in the below code?
HTML :
<div ng-app="">
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</div>`
and JS :
function HelloWorldCtrl($scope){
$scope.helloMessage="Hello World";
}
same problem when run in jsfiddle

var myapp = angular.module('myApp', []);
myapp.controller('HelloWorldCtrl', function($scope){
$scope.helloMessage = "Hello world";
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</div>

Related

Angular $scope value won't show up on a page

I am starting to learn Angular and tried to repeat the "To do list" task they have on the homepage. However, I am stuck in the very beginning:
http://codepen.io/Deka87/pen/grGWqQ
Template
<div ng-app>
<div ng-controller="TodoCtrl">
<h2>{{totalItems}}</h2>
</div>
</div>
Javascript
function TodoCtrl($scope) {
$scope.totalItems = 4;
}
I seem to do the very same thing, however it doesn't work. What's wrong?
First you must import the angular library, then instantiate your app and finally create your controller, this way:
angular.module('app', [])
.controller('TodoCtrl', function($scope) {
$scope.totalItems = 4;
});
<html ng-app="app">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>
<body ng-controller="TodoCtrl">
<div>
<h2>{{totalItems}}</h2>
</div>
</body>
</html>
I recommend you to check this: What is a Module?
http://codepen.io/anon/pen/EywXXQ Here is the fork of your codepen with the code working.
You need to import the angular script
Need to create an angular app like this
Inject the app in your html using ng-app
Create a controller and inject it in your html using ng-controller
angular.module('app', []).controller('TodoCtrl',
function ($scope) {
$scope.totalItems = 4;
});
Go through these tutorials that will help you understand it better
Tutorial

How to fix angular js ng-controller function call?

I created a basic angular js module where I am passing the module and the controller message in a separate script file. The issue I am facing is it does not print the message defined in the called function. This seems to me might be an issue with the ng-app defined.
Here is the code :
module.html
<!doctype HTML>
<html ng-app="myModule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src = "D:\Educational\angularJS code\script.js"></script>
</head>
<body>
<div ng-controller="myController">
{{ message }}
</div>
</body>
</html>
And here is the called script.
script.js
var myApp = angular.module("myModule",[]);
var myController = function($scope){
$scope.message = "Angular JS";
}
myApp.controller("myController", myController);
The only thing that gets printed in the format is {{message}} and NOT the actual message "Angular JS".
Any suggestions would be of great help.
Since you have your script.js in the same folder just include it simply by
<script src="script.js"></script>
make sure you have internet on your computer since you are using a CDN
Note: please check your console by pressing ctrl+j or f12 to see if there are any errors
You can also try putting your code into the markup itself for now and check if the code works fine.
<script>
var myApp = angular.module("myModule",[]);
var myController = function($scope){
$scope.message = "Angular JS";
}
myApp.controller("myController", myController);
<script>

Getting an angularjs error from a basic app

Code:
<!DOCTYPE html>
<html>
<head>
<!-- angular -->
<script src="angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="MainController">
<p>{{ title }}</p>
</div>
<!-- Modules -->
<script src="app.js"></script>
<!-- Controllers -->
<script src="MainController.js"></script>
<!-- Directives -->
<script src="timelineInfo.js"></script>
</body>
</html>
All my files are in the same folder because I was having a problem doing src="js/...".
I get the error:
angular.min.js:6 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0-rc.1/$injector/modulerr?p0=app&p1=Error%3…0%20%20at%20c%20(http%3A%2F%2Flocalhost%3A8080%2Fangular.min.js%3A20%3A42
I don't exactly understand what the website was suggesting. I'm using the angular.min.js in my index directory because linking it to the http file was giving me issues. All I'm trying to do is run a simple app so I can build from there but even that is giving me issues.
(I am running from a local machine)
Thanks!!
---------------------------------OTHER FILES----------------------
app.js:
var app = angular.module("timelineApp", []);
MainController.js:
app.controller('MainController', ['$scope', function($scope) {
$scope.title = 'this is a test';
}]);
The problem is that in your html you have app module referenced in ng-app. And in your app.js you have module name as timelineApp.
You need to sync them.
Either update your app.js
from
var app = angular.module("timelineApp", []);
to
var app = angular.module("app", []);
Or update your markup from
<body ng-app="app">
to
<body ng-app="timelineApp">
ng-app value in your html code should have the same as the angular module in your javascript.
For example in you case it should be following:-
<ng-app="timelineApp"> // in your html code
or following:-
var app=angular.module('app',[]); // in your javascript
Also, if you still face the error after that too, then you can one of my answers on the same here
The error you are getting refers to the fact that your application is not bootstrapped correctly. Hence Angular's compiler does not understand the directives it encounters; in this case, that would be ng-controller
It's important that you understand what's happening.
var app = angular.module("timelineApp, []")
and subsequently
<body ng-app="app">
ng-app should really be timelineApp since that is what you set the name of your angular module to.
The code above "bootstraps" your application. Once you application is bootstrapped, Angular's compiler will go through the page's markup. When it encounters ng-app directive, it will look at its value timelineApp and know that it is registered as an angular application.
This also help it understand that you have a controller registered on the app:
app.controller('MainController', ['$scope', function($scope) {
$scope.title = 'this is a test';
}]);

Angular JS ng-include

I Followed that link to learn how to use: ng-include: http://www.w3schools.com/angular/angular_includes.asp
But I Have few questions and I not understand well how it works.
If I remove app1.js = the ng include will not works, why? I really not understand angular I am just trying for the first time.
app1.js
angular.module('myApp', []).controller('userCtrl', function($scope) {
})
as well if I am not running this code in a server will not works too, why?
html code:
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body ng-app="myApp" ng-controller="userCtrl">
<div class="container">
<div ng-include="'includes/content.html'"></div>
<div ng-include="'includes/header.html'"></div>
</div>
<script src= "js/app1.js"></script>
</body>
</html>
you use the <body ng-app="myApp" ng-controller="userCtrl">
After you remove the app1.js there is no controller to match with the ng-controller so there will be a error saying undefined controller (check the console),
and change the ng-app="myApp" to ng-app, if you keep the ng-app="myApp" then it will search for a module called myApp as angular.module('myApp', [])
remove the ng-controller directive and check it will work.
then whole think would be
<body ng-app>...

angularJS $http.get communicating with API

I am new to AngularJS and I am trying to understand it by studying sample codes.
Here I have one about $http.get, from the following link:
http://www.w3schools.com/angular/tryit.asp?filename=try_ng_customers_json
I just replaced url with one of my own but it did not work, and I am really confused, please help, thanks!
=========================
second edit: thanks to the answers, double ng-app is a mistake, but it is not the main reason for this problem. I think it has something to do with cross-site blocking, but I have turn it off on my API (I use codeigniter REST API and I set $config['csrf_protection'] = FALSE; ), I am not sure how to configure it.
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="customersCtrl">
<ul>
{{names}}
</ul>
</div>
<div ng-controller="myCtrl">
<ul>
{{names}}
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/website/Customers_JSON.php")
.success(function (response) {$scope.names = response;});
});
app.controller('myCtrl', function($scope, $http) {
$http.get("https://manage.pineconetassel.com/index.php/api/v1/colors2.php")
.success(function (response) {$scope.names = response;});
});
</script>
</body>
</html>
The problem is that you have two "myApp" declarations.
From AngularJS documentation:
Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application.
So, you should move the ng-app="myApp" to the body element.
However, once you've done that you probably still won't see any result because you are cross-site scripting and browsers will (by default) block the second request.
Two ng-app directive on single page will execute the first one, 2nd one will get ignored.
Remove ng-app="myApp" from both div of your html and use angular.bootstrap to bootstrap angular app on html page using below code.
Code
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});

Categories

Resources