0

I'm building the app in AngularJS 1.5.8.
I've a main root component called app.component.
in app.component, I wanna watch all routes changes which happens navigating between child components.
And in watch, I need to get routeParams, as well.

I've defined the routes via $routeConfig in angularjs component-router

I've tried with this.$routerOnActivate but it wasn't called because I'm on root component.

var controller = function ($scope) {
    $scope.$on('$routeChangeSuccess', (event, data) => {
        // This is called everytime route is changed
        // Here, I need to get the routeParams 

        // Not sure this is the correct way to watch route changes in angular component-router  
    });
}

angular
    .module('example')
    .component('RootComponent', {
        templateUrl: 'app/components/enter/enter.component.html',
        controller: controller,
        $routeConfig: [
            {path: '/a', name: 'AComponent', component: 'AComponent', useAsDefault: true},
            {path: '/b', name: 'BComponent', component: 'BComponent'},
        ]
    });

Thanks.

PS
I've tried with How to watch for a route change in AngularJS?.
It was called everytime I change the route but I can't get routerParams from it.
On the official doc, it said 3 params are available - $event, current, next but only $event is coming, the rest is undefined.
I thinks it's working when we define routes via $routeProvider but not in my case.

Patrick Blind
  • 399
  • 1
  • 4
  • 15
  • I think you are confused between AngularJS and Angular 4 – Akash Agrawal Dec 21 '17 at 12:51
  • 2
    Possible duplicate of [How to watch for a route change in AngularJS?](https://stackoverflow.com/questions/14765719/how-to-watch-for-a-route-change-in-angularjs) – Brian Dec 21 '17 at 13:01
  • @AkashAgrawal why he would be? – Zooly Dec 21 '17 at 13:02
  • are you using `ui.router` or `ngRoute`? – Stanislav Kvitash Dec 21 '17 at 13:02
  • I'm using angularjs component-router. via $routeConfig in angularjs component – Patrick Blind Dec 21 '17 at 13:16
  • @Brian I've checked that article already and it works but I can't get routerParams from it. on the official doc, it said 3 params are available - $event, current, next but only $event is coming, the rest is undefined. I thinks it's working when we define routes via $routeProvider but in my case. – Patrick Blind Dec 21 '17 at 13:18
  • You should show us some code, and which router you are using. Please see https://stackoverflow.com/help/mcve for more details. The question as it stands doesn't tell us enough. – Brian Dec 21 '17 at 13:23
  • @Brian Updated the description, Thanks – Patrick Blind Dec 21 '17 at 13:29
  • So what are you actually trying to accomplish? The way this is defined, I am guessing you just want to use `$routeParams`. https://stackoverflow.com/questions/11758079/how-to-get-the-url-parameters-using-angularjs/16672520#16672520 – Brian Dec 21 '17 at 14:00
  • I can't use $routeParams since I don't use angular-router, I'm using component-router. – Patrick Blind Dec 21 '17 at 20:30

0 Answers0