0

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?

StuperUser
  • 10,555
  • 13
  • 78
  • 137
  • Try using the HTML5 mode along with the Hashbang mode. See: [this answer](http://stackoverflow.com/a/16678065/4623467). – alesc Jun 11 '15 at 18:05
  • @robin Giltner I've added the kendo-ui tag back since I'm integrating this app to a page that is part of a kendui-ui app. – StuperUser Jun 11 '15 at 18:07
  • 1
    I got you now @StuperUser, since this will be sitting on top of KendoUI's Mobile routing already in place. Good call. – Robin Giltner Jun 11 '15 at 18:30

1 Answers1

0

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];
}
StuperUser
  • 10,555
  • 13
  • 78
  • 137
  • You could also use the $location service which is angular's abstraction of `window.location`. – Joao Leal Jun 17 '15 at 13:53
  • I was having trouble injecting that into my controller. Could you provide an example of how to use that @JoaoLeal? – StuperUser Jun 17 '15 at 13:54
  • 1
    Can you see if this helps, it's a bit weird with plnkr: http://plnkr.co/edit/VZGQgNhC48V52q3mVkwr?p=preview and https://docs.angularjs.org/guide/$location – Joao Leal Jun 17 '15 at 14:48