I'm working on a new Angularjs webapp where I have to use ngRoute. I'm a bit confused first, because routing doesn't work at all in my case – I always end up at index
and MainCtrl.
in index.html I've included all dependencies like so:
<body ng-app="orderyourselfApp">
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<div class="container" ng-controller="LoginCtrl">{{hello}}</div>Soem random stuff
<!--[if lt IE 9]>
<script src="bower_components/es5-shim/es5-shim.js"></script>
<script src="bower_components/json3/lib/json3.min.js"></script>
<![endif]-->
<!-- build:js(app) scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<!-- endbower -->
<!-- endbuild -->
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/navbar.js"></script>
<!-- endbuild -->
</body>
And I have the routes set up in app.js
'use strict';
angular.module('orderyourselfApp', [
'ngResource',
'ngRoute'
])
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'partials/main',
controller: 'MainCtrl'
})
.when('/login', {
templateUrl: 'login',
controller: 'LoginCtrl'
})
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
And ofcourse, main.js looks like this:
'use strict';
angular.module('orderyourselfApp')
.controller('LoginCtrl', function ($scope, $http) {
console.log('saysomething');
$scope.hello = 'Hello world! LoginCtrl';
})
.controller('MainCtrl', function ($scope, $http) {
console.log('shit is getting real');
$http.get('/api/awesomeThings').success(function(awesomeThings) {
$scope.awesomeThings = awesomeThings;
});
});
Now, the issue is weird and simple: when I do a localhost:9000/login, I should get to the login.html template and LoginCtrl, but no. I keep ending up at index
and MainCtrl
and nothing seems to change the result.
login.html
<body ng-app="orderyourselfApp">
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<div class="container" ng-controller="LoginCtrl">{{hello}}</div> … then the usual stuff
Where's this going wrong?
First off, templateUrl
should point to your actual partial's relative path, for example: templateUrl: 'partials/login.html'
.
Then, I think you should first land on http://localhost:9000/
and then follow a link to http://localhost:9000/login
Anyway, you partials should not contain the ng-app
declaration (they actually are partial). So you should have an index.html
that would define your page's template, and two partials: main.html
and login.html
, neither of which should contain the ng-app
declaration.
Ending in something like this:
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'partials/main.html',
controller: 'MainCtrl'
})
.when('/login', {
templateUrl: 'partials/login.html',
controller: 'LoginCtrl'
})
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
With index.html being the page's landing from http://localhost:9000/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With