I have a problem with the AngularJS routing: I don't get any feedback of the page. No errors or view-switches.
I checked my implementation of the module, but it's declared in the right way. Then I searched for typos such as templateURL
, but I didn't find any. I also tried to use ng-href
instead of href
in the list, but then the links weren't clickable anymore.
And my code:
Created my navigation:
<body ng-app="Productportfolio"> <ul> <li> <a href="#/home">Home</a> </li> <li> <a href='#/privat'>Log in</a> </li> </ul> <ng-view></ng-view> <!--own js --> <script src="app.js"></script> <!--Controller --> <script src="ProductCtrl.js"></script> <!--Services --> <!--Direktives--> </body>
Made the templates:
//home.html <div> <h1> Home </h1> </div> //private.html <div> <h1> Private</h1> </div>
Declared a Angular module:
angular.module('Productportfolio', ['ngRoute'])
Added the $routeProvider to my config:
angular.module('Productportfolio', ['ngRoute', 'ProductService', 'ProductCtrl']) .config(['$routeProvider, $locationProvider', function ($routeProvider, $locationProvider) { $routeProvider .when('/home', { templateUrl: 'home.html', controller: 'ProductCtrl' }) .when('/private', { templateUrl: 'private.html', controller: 'ProductCtrl' }) .otherwise({ redirectTo: '/home', controller: 'ProductCtrl' }); $locationProvider.hashPrefix('!'); }]);
In your Plunker, there were some issues related to imports. To make it easy, I simply removed both jQuery and Bootstrap. I also put all your modules in a single app.js file.
There were some errors in your html:
Angular
BEFOREngRoute
or any other moduleng-href='#routeName'
or, otherwiseAnd in your js:
Notice that, to make it easy, I removed also your Service since you did not share it and it was useful.
Last but not least, if you want to use
$locationProvider.hashPrefix('!');
to usehashbangs
, you need to add<base href="/" />
at the end of yourhead
.For some reason, plunker does not allow me to do that, but I'm sure you'll get it to work on your version.
Here You can find the working plunker reproducing your application.
Hope I've been helpful.
The remaining and not visible problem is here :
config()
function of module object takes as parameter, a array of strings and not a string with "," as separator char inside it. See example and doc here : https://docs.angularjs.org/guide/providers#provider-recipeSo, it should be :
Update :
But in fact, in your case, it works even without precising the array :
I updated the plunker and the app.js with both versions. I have the impression that the array is not mandatory (any longer). So, it seems better to ignore that parameter, especially, if with bad value, it may create side-effects.
Here the plunker with corrections (ordered lib, typos and config function called) : http://plnkr.co/edit/NTn6Zmav5RX4V8zgHPOG?p=preview
I have removed
$locationProvider.hashPrefix('!')
as not suitable for the url you are using. See @AndreaM16 answer for that.Besides, you have not declared your service you want to use in your controller.
app.js
or app.js without the array parameter in
config
function :index.html :
The order of loading java script files is very important. Load in the following order:
It means that you are loading
Bootstrap.js
files, but Bootstrap cannot work withoutjQuery
library. So it means you should loadjQuery
at first, thenBootstrap.js
. And other libraries should be reordered(I've shown in an example above).In addition, you can use
Console
in your browser to see whether there are errors and what errors you have.