0

I am unable to route the path code/:code in my website. I receive the following error:

Firefox can't find the file at /C:/Code/dola/mobileweb/app/code/123

default.htm is located in the app folder.

I am using the mobileangularui framework. I have the following routes declared (among others):

app.config(function ($routeProvider) {
    $routeProvider.when('/', { templateUrl: 'codeview.htm', reloadOnSearch: false });
    $routeProvider.when('/code/:code', { templateUrl: 'codeview.htm', reloadOnSearch: false });
});

Everything works fine, except when I try e.g. code/12345.

This route is intended for a call from outside, e.g. www.mywebsite.com/code/12345.

Corne Beukes
  • 1,759
  • 4
  • 14
  • 18
  • Your templateUrl is relative to the path. it's looking for 'codeview.htm' in '/code/:code', because you provided a file name without a path. – Neil S Jul 30 '15 at 18:37

1 Answers1

1

I see two issues. First is re-declaring the $routeProvider unnecessarily. The second is you don't have an "otherwise" clause to catch everything you aren't already. Also, not an error, but good practice to follow; you are missing a controller and controllerAs explicit directive for each entry. Try this:

app.config(function ($routeProvider) {
$routeProvider
  .when('/', {
     templateUrl: 'codeview.htm'
     , controller: 'HomeController'
     , controllerAs: 'vm'
     , reloadOnSearch: false
  })
  .otherwise({
     redirectTo: '/'
  })
});

This was based on the assumption that '/code/:code' is not its own page. The option is easy to add/change if this is not the case, following the above model.

Hope this helps.

-C§

-edit-

It looks as though the best way to do this (as with what I've done in my current project for such needs) is to store the code into a global (model) variable and call the redirect using $location.path('/');. Something like the below would be part of the click-function:

DataService.code = vm.code; //the code value selected
$location.path('/');

And the resulting page would have as part of its initialization routine:

vm.code = DataService.code; //retrieves the code from memory
//and uses it to populate the page appropriately

I'm unfamiliar with how to specify a URL and have it read what page it should populate based on the trailer, aside from using PHP to catch the URL extension with the index and a $_GET redirection process. I know this can be achieved using AJAX or a Jquery catch, but I'm still somewhat new to JavaScript as a whole and not sure what the process would look like.

Alternatively, you could designate a page for each possible code (ex: 12345.html || 12345.php).

Hope this also helps.

-C§

CSS
  • 412
  • 5
  • 17