2

I have my backend web framework loading my AngularJS app with following URL

http://localhost/New/Alpha/App

I also have it set up so that anything after App will still load the same thing

http://localhost/New/Alpha/App/home
http://localhost/New/Alpha/App/settings
...

I'm trying to make my AngularJS app to work in the way that it would pick up the bit of URL after App and load a controller/template accordingly. I have a problem with routing in my AngularJS app though

var main = angular.module("main", ["ui.bootstrap", "ngRoute"]);

main.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when("home", {
        templateUrl: "assets/tpl/home.html",
        controller: "mainController"
    })
    .otherwise({
        redirectTo: "fail"
    });
    $locationProvider.html5Mode(true);
});

main.controller("mainController", function($scope) {
    console.log("home")
});

If I try this URL

http://localhost/New/Alpha/App/home

it changes the URL to

http://localhost/fail

instead of leaving the URL as it is and loading the template/controller. If however I change the config and give it a full relative URL it does work as supposed to

.when("/New/Alpha/App/home", {
    templateUrl: "assets/tpl/home.html",
    controller: "mainController"
})

My problem is, that the part of URL before App - /New/Alpha cannot be hardcoded in. It could be /New/Beta, /New/Gamma, etc.

Is what I want to do possible at all without hardcoding the full relative URL match?

UPDATE Sorry, forgot to mention that the number of URL segments before App can change, as in it could be /New/Beta/App and it also could be /New/Another/Beta/App. I don't suppose something like */App or /New/*/App is possible instead of /New/:placeholder/App?

2
  • try this localhost/#New/Alpha/App/home Commented Apr 16, 2014 at 15:21
  • @Dalorzo This particular URL would be handled by my backend framework, only http://localhost/New/Alpha/App and anything after it loads AngularJS app Commented Apr 16, 2014 at 15:25

2 Answers 2

2

Will this work for you?

var main = angular.module("main", ["ui.bootstrap", "ngRoute"]);

main.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when("/New/:greek/App/home", {
        templateUrl: "assets/tpl/home.html",
        controller: "mainController"
    })
    .otherwise({
        redirectTo: "fail"
    });
    $locationProvider.html5Mode(true);
});

main.controller("mainController", function($scope) {
    console.log("home")
});

You could then retrieve the greek with $routeParams.greek from within your controller.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this looks like the right direction, although I forgot to mention that the number of URL segments before App can change, as in it could be /New/Alpha/App or it could be /New/Another/Something/App. I don't suppose something like this is possible - */App/home?
Angular does not currently support regular expressions in routes, so you will need to specify them manually.
@Romoku I see. What about 3rd party then, like github.com/angular-ui/ui-router ?
0

The general solution to this problem is to have the server pass the app URL to your client-side code. In other words, use server-side code to dynamically write the equivalent of the following on the page:

var appUrl = '/New/Alpha/App';

Then setting up the route provider becomes:

main.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when(appUrl + "/home", {
        templateUrl: "/assets/tpl/home.html",
        controller: "mainController"
    })
    .otherwise({
        redirectTo: appUrl + "/fail"
    });
    $locationProvider.html5Mode(true);
});

That way the knowledge of the application base URL lives in one place — server-side (which makes sense as only the server is in a position to truly know, if you think about it).


In your specific case, if the application base URL is implicit in the URL structure, you could calculate the following client-side:

var appUrl = window.location.pathname.match(/^\/New\/.*\/App/);

Needs work, but you get the idea. Then you can set up the route provider exactly as above.

Comments

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.