0

So I'm trying to use angular routing in my application. The problem is that the route "/" does not get initialized automatically. So I first go to "/" and the main content of the page is empty (the template is not loaded at all), I click the link to /settings and settings are loaded, then I click a link to "/" and this time the template is initialized correctly. So how do I make the thing initialized at the beginning?

Here's my routing configuration:

 app.config(function($routeProvider, $locationProvider) {
          $routeProvider
           .when('/', {
            templateUrl: '/profile.html',
            controller: 'ProfileController',
          }).when('/profile', {
            templateUrl: '/profile.html',
            controller: 'ProfileController',
          }).when('/settings', {
            templateUrl: '/settings.html',
            controller: 'SettingsController'
          });

          $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
          });
        });

I tried calling $location.url("/profile") from a controller and it did help, but the url has to be changed and I would rather keep the "/"

4
  • Try removing the trailing commas in your .when()'s for a start Commented Sep 12, 2015 at 14:54
  • 1
    doesn't help. Also, as far as I know it is perfectly okay to have a trailing comma in your property listing in JS Commented Sep 12, 2015 at 14:59
  • In some cases it can break things. So you shouldn't leave trailing commas just for the sake of it. It was merely a pointer rather than an answer :) Commented Sep 12, 2015 at 15:01
  • have you try to read this answer Commented Sep 12, 2015 at 15:20

1 Answer 1

1

use otherwise({ redirectTo: "/" })

Update : here how it should be

 $routeProvider
           .when('/', {
            templateUrl: '/profile.html',
            controller: 'ProfileController',
          }).when('/profile', {
            templateUrl: '/profile.html',
            controller: 'ProfileController',
          }).when('/settings', {
            templateUrl: '/settings.html',
            controller: 'SettingsController'
          }).otherwise({ redirectTo: '/' });
Sign up to request clarification or add additional context in comments.

4 Comments

looks like something I should add to my code anyway, but doesn't really solve this particular problem :(
I did a dirty hack... first I configured the "otherwise" and then I did the $location.url("/whatever"). Now the page is initialized nicely :P
just remove "/" from all your templateURL
doesn't help. I'm gonna stick to my solution inspired by your original advice :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.