0

I'm new to angularjs and want to integrate it in a cakephp app. For some pages I don't have a controller since no javascript is exectuted there or because I still have to create them. I however don't want to list them all in the routes. For this reason i set it like the following:

angular.module('desktop', []).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $locationProvider.html5Mode(true);

  $routeProvider
    .when('/', {templateUrl: 'pages/index',   controller: IndexController})
    .when('/clubs', {templateUrl: 'partials/clubs.html',controller: ClubListController})
    .otherwise({templateUrl: location.pathname});
}]);

This is however not working. When I go to /help, nothing happens. What am i doing wrong?

bicycle
  • 8,315
  • 9
  • 52
  • 72
  • Are you looking for something like `.otherwise({ redirectTo: '/' });` – Chandermani Sep 17 '13 at 13:18
  • I'm looking at it again and I don't think you want to set templateUrl to the very page you're on – m.e.conroy Sep 17 '13 at 13:19
  • @Chandermani that only retrieves the 'pages/index' which i don't want – bicycle Sep 17 '13 at 13:21
  • @m.e.conroy when i do that it gives me this error `Uncaught Error: Unknown provider: $location from desktop`. I'm not sure what you mean about the other comment... – bicycle Sep 17 '13 at 13:23
  • @m.e.conroy basically when no routes for a url is defined, i just want it to load that deeplink if you understand what i mean. There are some pages that don't have any javascript or i still need to create it. – bicycle Sep 17 '13 at 13:27
  • @m.e.conroy it is weird that `.when('/help', {templateUrl: 'help'})` works but `.otherwise({templateUrl: location.pathname})` or `.otherwise({templateUrl: location.pathname.substring(1)()})` doesn't – bicycle Sep 17 '13 at 13:30
  • 1
    $location.path() and inject the $location in your config param like $routeProvider – Thomas Pons Sep 17 '13 at 16:18
  • As far as I have understood it there is currently no way to do this just with the routes. One route will only ever serve one view. But people have been working around it using includes, which might work for you depending on what you are after. See this question and the accepted answer for an example of how this works: http://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl?rq=1 – Erik Honn Sep 17 '13 at 20:51
  • Also, the routes are defined whe the app starts, so using location.pathname won't work since it will be set to whatever the value is when the app starts and then never changed again. It does not reload per route. – Erik Honn Sep 17 '13 at 20:52
  • @ErikHonn thanks, there is no way i could have found that one. That really did the trick. Thanks also for the good explanation. If you put your comment as an answer i will accept it and upvote it – bicycle Sep 18 '13 at 01:06
  • Done, also added a tiny hint about full browser reloads, since that can be done to force routes like this to work. Wouldn't recommend it though :) – Erik Honn Sep 18 '13 at 13:12

1 Answers1

1

From my comments:

As far as I know it there is currently no way to do this with just routes. Routes are made to be static, they are defined as the app loads and do not update dynamically as time goes by. So using location.pathname() (or directly checking window.location) won't work since the route be set to whatever the value is when the app starts, and then never change again. It won't update when you load a new path unless you do a full browser reload (this is btw possible, but a hacky sollution).

But people have been working around it using includes, which might work for you depending on what you are after. See this question and the accepted answer for an example of how this works.

AngularJS - How to use $routeParams in generating the templateUrl?

Community
  • 1
  • 1
Erik Honn
  • 7,576
  • 5
  • 33
  • 42