How can I access non-route url parameters in an angular app?

191 views Asked by At

I am adding an angular app to part of an existing page that uses a lot of kendo MVVM controls in an ASP.NET MVC app. Lucky me.

I want to access a parameter from the URL route that is before the angular route.

So for a module with:

angular
    .module('myModule', ['someCommonControls', 'ngRoute', 'aConfigModule'])
    .config(function ($routeProvider) {
        $routeProvider
            .when('/Something/Something/Details/:somethingId', {
                templateUrl:'/path/to/templates/awesome.html',
                controller: 'awesome',
                controllerAs: 'awesome'
                }
            })
            .otherwise({ redirectTo: '/awesome' })
        ;
});

When I hit http://localhost/Something/Something/Details/something-id-guid-deadbeef
the page loads and it contains my app and the otherwise routes to http://localhost/Something/Something/Details/something-id-guid-deadbeef#awesome

Now, the route is based on #awesome, so doesn't contain somethingId for me to push to my controller ctor.

How can I access it and pass it to the ctor?

1

There are 1 answers

3
StuperUser On BEST ANSWER

Since I was essentially getting a value out of the url, I used the old school approach until I can use routes across the entire app properly:

ctrl.theGuid = '';
if (document.location.pathname.indexOf('/Something/Something/Details') != -1) {
    //TODO update this to the routing engine when possible
    //0:first '/', 1:Something, 2:Something, 3:Details, 4: guid
    ctrl.theGuid = document.location.pathname.split('/')[4];
}