Reputation: 25
So, I am really new to angular JS and I'm working on a project for a company with a team from my school as an internship. I have to get the routing working first though, and it just will not work no matter what I have tried. I tried it with angular JS 1.0.7 and with that I could get it to work, but with the newest version (1.5.7) I just can't after many many hours of trying. This is the error the console currently gives me:
Error: $injector:nomod Module Unavailable Module 'app' 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. The error originates from the line in index.html as far as I know, since the error disappears if i remove it, but the routing doesn't work then either.
I'd really appreciate any help with this. I know I probably missed something incredibly basic (and the code is probably pretty sloppy), but I just can't figure it out. Oh and by the way, I tried putting the content from app.js to index .html inside script tags and then it didn't give the error, but didn't display anything either, really weird. I have also tried many different ways to do routing as instructed by different tutorials or guides, but I still can't get it to work.
Code: Index.html (modified so as to not give way the name of the company etc.)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/ui-bootstrap-1.3.3-csp.css" rel="stylesheet">
<link href="css/main.css" rel="stylesheet">
<script src="js/angular.min.js"></script>
<script src="js/angular-resource.min.js"></script>
<script src="js/angular-route.min.js"></script>
<script src="js/ui-bootstrap-1.3.3.min.js"></script>
<script src="js/angular-animate.min.js"></script>
<script src="js/angular-touch.min.js"></script>
<script src="app/app.js"></script>
<script src="app/ctrl.js"></script>
<script src="scripts/routing.js"></script>
</head>
<body>
<div class="navbar navbar-default" id="navbar">
<div class="container" id="navbar-container">
<div class="navbar-header">
<a href="" class="navbar-brand">
<small>
<i class="glyphicon glyphicon-log-out"></i>
Products and Company Info
</small>
</a><!-- /.brand -->
</div><!-- /.navbar-header -->
<div class="navbar-header pull-right" role="navigation">
<a href="#howto" class="btn btn-sm btn-warning nav-button-margin"> <i class="glyphicon glyphicon-book"></i> Howto</a>
<a href="#" class="btn btn-sm btn-warning nav-button-margin"> <i class="glyphicon glyphicon-home"></i> Home</a>
</div>
</div><!-- /.navbar-container -->
</div><!-- /.navbar -->
<div>
<div class="container">
<div ng-app="app">
<h2>App name</h2>
<br/>
<div ng-view></div>
</div>
</div><!-- /.container -->
</div>
</body>
</html>
Code app.js:
var app = angular.module('mainApp', ['ngResource', 'ngRoute']);
app.run(function($rootScope) {
});
Code ctrl.js
app.controller("mainCtrl", function($scope) {
$scope.firstName = "first name";
$scope.lastName = "last name";
});
app.controller('howtoCtrl', function($scope) {
$scope.message = 'How To guide here';
});
Code routing.js:
// configure our routes
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'home.html',
controller : 'mainCtrl'
})
// route for the how to page
.when('/howto', {
templateUrl : 'howto.html',
controller : 'howtoCtrl'
})
// route for the test2 page
.when('/testi2', {
templateUrl : 'test2.html',
controller : 'test2Controller'
});
});
Edit: After changing ng-app="app" to ng-app="mainApp", I got the following errors
angular.min.js:103 GET http://localhost/var/www/html/byow_routing_sahlays_2/byow/home.html 404 (Not Found)(anonymous function) @ angular.min.js:103n @ angular.min.js:98g @ angular.min.js:95(anonymous function) @ angular.min.js:130$eval @ angular.min.js:145$digest @ angular.min.js:142$apply @ angular.min.js:145(anonymous function) @ angular.min.js:20invoke @ angular.min.js:41c @ angular.min.js:20Bc @ angular.min.js:21ge @ angular.min.js:19(anonymous function) @ angular.min.js:315b @ angular.min.js:189Sf @ angular.min.js:37d @ angular.min.js:36 angular.min.js:117 Error: [$compile:tpload] http://errors.angularjs.org/1.5.7/$compile/tpload?p0=home.html&p1=404&p2=Not%20Found at Error (native) at http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:6:412 at http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:156:281 at http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:130:409 at m.$eval (http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:145:107) at m.$digest (http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:142:173) at m.$apply (http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:145:401) at l (http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:97:250) at K (http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:101:373) at XMLHttpRequest.y.onload (http://localhost/var/www/html/byow_routing_sahlays_2/byow/js/angular.min.js:102:397)
Final Edit: Thanks guys, it works now. The problem was first that ng-app="app" should have been ng-app"mainApp", and I forgot to include the folder to the address of the tempaltes in routing.js after I had originally moved the files to their own folder.
Upvotes: 2
Views: 2586
Reputation: 2878
You call in your html <div ng-app="app">
but your app is named mainApp
.
Try to change <div ng-app="app">
to <div ng-app="mainApp">
Upvotes: 1
Reputation: 3520
Add below code to the very first line of ctrl.js
and routing.js
var app = angular.module('mainApp');
cheers!
Upvotes: 0