Seperate AngularJS controller files with local HTML page - javascript

I am trying to run a local webpage with AngularJS with dynamically driven controllers. I'd like a URL variable to drive which particular javascript file is loaded with data to drive my page.
HTML File
<html>
<head >
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js'></script>
<script>
var app = angular.module("myApp", []);
</script>
<script>
var url = new URL(window.location.href);
var t_var= url.searchParams.get("t_var");
var x = document.createElement('script');
x.src = t_var +'.js';
document.getElementsByTagName("head")[0].appendChild(x);
</script>
</head>
<body ng-app='myApp' ng-controller='Ctrl1'>
{{sub1Variable}}
</body>
</html>
AngularJS controller
app.controller("Ctrl1", function($scope) {
$scope.sub1Variable = 'sub1'
});
I have been able to get this to work if I include the below tag in the HTML file.
<script src="sub1.js"></script>
I keep receiving an error that a controller with this name is not referenced.

In principle what you are doing here should be fine. See below.
app.controller("Ctrl1", function($scope) {
$scope.sub1Variable = 'sub1'
});
<html>
<head >
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js'></script>
<script>
var app = angular.module("myApp", []);
</script>
</head>
<body ng-app='myApp' ng-controller='Ctrl1'>
{{sub1Variable}}
</body>
</html>
If you run that you should see 'sub1' in the output.
So, if you are receiving the error saying that your controller isn't registered then almost certainly the problem is you aren't loading the JavaScript file that contains your controller registration. Make sure the script that contains the app.controller... line is included through a <script> element at some point after loading angular and registering the module.
(Remember the SO Snippet and CodePen etc will implicitly include the JavaScript for convenience, in real code we need to do it ourselves).
In fact, if this is just a quick local app maybe you can just include it in the same script element as the module registration, like so:
<html>
<head >
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js'></script>
<script>
var app = angular.module("myApp", []);
app.controller("Ctrl1", function($scope) {
$scope.sub1Variable = 'sub1'
});
</script>
</head>
<body ng-app='myApp' ng-controller='Ctrl1'>
{{sub1Variable}}
</body>
</html>

Related

Angular controller directly in index.html

I need to make a one page app without any additional files like app.js or controller.js
I created the example below, but I get the error:
Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> JavaScript Angular</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js">
var myАpp = angular.module("myАpp", []);
myАpp.controller("firstController", function($scope){
$scope.test = "test-to-test";
});
</script>
</head>
<body ng-app = "myApp">
<div ng-controller = "firstController">
{{test}}
</div>
</body>
</html>
So, is it possible to have only one file index.html with Angular and still have a controller inside?
You need to have the controller and module inside the <script> tag
<div ng-app="myАpp">
<div ng-controller="firstController">
<h1>{{test}}</h1>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script>
var app =
angular.module('myАpp', []).controller('firstController', function($scope){
$scope.test = "test-to-test";
});
</script>

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 script is not executing [duplicate]

I am learning Angular.js and I am not able to figure out whats wrong with this simple code. It seems to look fine but giving me following error.
**Error**: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.14/$injector/modulerr?p0=app&p1=Error%3A%20…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.14%2Fangular.min.js%3A17%3A381)
And before adding ng-app="app" (I was just keeping it as ng-app) it was giving me following errors. Why is that?
Error: [ng:areq] http://errors.angularjs.org/1.3.14/ng/areq?p0=Ctrl&p1=not%20a%20function%2C%20got%20undefined
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:6:417
at Sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:19:510)
at tb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:20:78)
at $get (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:75:331)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:57:65
at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:7:408)
at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:56:443)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:299)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)
<!doctype html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<div ng-controller="Ctrl">
<input ng-model="name">
<h1>{{name}}</h1>
<h2>{{age}}</h2>
</div>
<script>
var Ctrl = function($scope)
{
$scope.age = 24;
};
</script>
</body>
</html>
After AngularJS version 1.3 global controller function declaration is disabled
You need to first create an AngularJS module & then attach all the components to that specific module.
CODE
function Ctrl($scope) {
$scope.age = 24;
}
angular.module('app', [])
.controller('Ctrl', ['$scope', Ctrl]);
Specifically for your case, there is some issue with AngularJS 1.3.14 (downgrade it to 1.3.13 works fine). Though I'd prefer you to use angular 1.2.27 AngularJS 1.6.X, Which is more stable version & latest release of AngularJS.
Working Plunkr
UPDATE:
You could do your current code to working state by allow global controller declaration inside angular.config. But this isn't the correct way to run angular application.
function Ctrl($scope) {
$scope.age = 24;
}
angular.module('app', [])
.config(['$controllerProvider',
function ($controllerProvider) {
$controllerProvider.allowGlobals();
}
]);
I was myself stuck in this issue for sometime. Check for following in order:-
The path to you angular.js script is correct (whether you are calling it in your HTML from a local source or as an external resource).
Next, once your angular.js is correct check if your app is initialized or not.
var app=angular.module('app',[])//in your app.js file
<body ng-app="app">//in your html
Next register your controller with the app and pass in all necessary injections
app.controller('myCtrl',function(){});
Call your javascript file in your html file
<script src="app.js"></script>
You have to define your controller
var app = angular.module('app', []);
app.controller('Ctrl', ['$scope',function($scope) {
$scope.age = 24;
}]);
Be Sure that ng-app="app_name" define must match to
var app=angular.module('app_name',[])
<html ng-app="myApp">
<body>
<div ng-controller="Ctrl">
<input ng-model="name">
<h1>{{name}}</h1>
<h2>{{age}}</h2>
</div>
<script>
var app = angular.module('myApp',[]); // same to the above define appName
app.controller('Ctrl',function($scope){
$scope.age=24; // initialize age by injecting scope
});
</script>
</body>
<html>
For more details visit Here
Check the version of angularjs in your html file.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
Call your javascript file in your HTML file
<script src="app.js"></script>

Simplest way to use ng-include

I have been trying to write a simple angular.js app, that includes another file using ng-include. I must have done something/missed something so that the included file does not get included. I must be doing something fundamentally wrong but can't see what it is.
Here is Index.html
<!DOCTYPE html>
<html>
<head ng-app="app">
<title>Demo</title>
<script data-require="angular.js#1.2.0" data-semver="1.2.0" src="http://code.angularjs.org/1.2.0/angular.js"></script>
<script data-require="angular-resource#1.2.0" data-semver="1.2.0" src="http://code.angularjs.org/1.2.0/angular-resource.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div ng-controller="appctrl">
<h1>Airports</h1>
<div ng-include="'extra.html'"></div>
<h1>Airports</h1>
</div>
</body>
</html>
file extra.html contains the element to be included
<h2>Inside</h2>
and script.js is the file to initialise angular
(function () {
'use strict';
var app = angular.module('app', []);
app.run(['$route', function ($route) { // Include $route to kick start the router.
}]);
angular.module('app').controller('appctrl', [function AppCtrl() { }]);
});
I created to Plunk to demonstrate what I mean. There's not much there: the ng-app defined, references to the angular files and a couple of titles to delineate the insert point. Nothing is shown between.
What is the simplest way to use ng-include in angular.js?
Place ng-app on your body section.
Include angular-route.js (eg. <script src="http://code.angularjs.org/1.2.0/angular-route.js"></script>) and add this dependency to your app initialization (var app = angular.module('app',['ngRoute'])
Don't put your script.js code inside (function(){}). (If you want to do this, call this function (function(){})();)
Your code with changes: http://plnkr.co/edit/k1dFPeb9E2B2pjTthi1K?p=preview
This is a script of mine:
HTML:
<div id="superhero-nav-container">
<ul id="superhero-nav">
<li><a ng-click='template = templates[0]'>Biography & Stats</a></li>
<li><a ng-click='template = templates[1]'>History</a></li>
<li><a ng-click='template = templates[2]'>Powers</a></li>
<li><a ng-click='template = templates[3]'>Enemies</a></li>
</ul>
</div>
<div class="superhero-templates" ng-include src='template.url'></div>
JS in the controller:
$scope.templates = [
{ url: '/views/superheroes/biography.html' },
{ url: '/views/superheroes/history.html' },
{ url: '/views/superheroes/powers.html' },
{ url: '/views/superheroes/enemies.html' }
];
$scope.template = $scope.templates[0];
I have something like menu that changes the html in the bottom div. Hope that helps.
EDIT (for the comment below):
Your mistakes were:
Put your ng-app attribute in the html tag of the
document not in the head
Include angular-resource.js (eg. <script src="https://code.angularjs.org/1.2.15/angular-resource.js"></script>) and add this dependency to your app initialization:
angular.module('app', ['ngResource']) ...
Don't put your script.js code inside (function(){}). If you want to do this, call this
function (function(){})();
Here is your working code: Plunker

Categories

Resources