Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribe to data change in Angular 2 based on route navigation

Suppose I have the following routes:

[
    {
        path: 'view',
        children: [
            {

                path: ':id',
                component: CustomerViewComponent,
                resolve: {
                    customerViewData: CustomerResolve,
                    edit: ViewRouteResolve // Returns false
                },
                data: {
                    other: false
                },
                children: [
                    {
                        path: 'edit',
                        resolve: {
                            edit: EditRouteResolve // Returns true
                        },
                        data: {
                            other: true
                        }
                    },
                    {
                        path: '',
                        data: {
                            other: false
                        }
                    }
                ]
            }
        ]
    },
    { path: '', component: CustomerListComponent }
]

I want to use the CustomerViewComponent for /view/ and /view/1/edit

The problem is I am unable to catch this data change in my component. I have tried with the resolve or data and I can't catch any changes...

This code will not trigger as expected:

this.route.data.subscribe(m => {
    debugger; // Triggers only once the view loads, then never again
});

// This triggers quite often, but the data is always stale.
this.router.events.subscribe(m => {
    console.log(this.route.snapshot.data['edit']);
    console.log(this.route.snapshot.data['other']);
    debugger;
});

Could this be a bug? my only work around is to look at the event NavigationEnd and analyze the .url string property...

like image 213
jsgoupil Avatar asked Nov 10 '16 18:11

jsgoupil


People also ask

How to detect the change in url route in angular?

Detect the change in URL route in NavigationStart’s subscribe method. We can add progress spinner or progress bar whenever a route change detected in Angular applications. Now we will go through an example to understand it further. I have created an Angular app which contains three routes. Aboutus,Services and Contactus.

How to implement same route refresh event in angular router?

The first approach is to tell the Angular router to emit the route events on refresh and then handle them accordingly in our routed component. In early Angular versions, there was no option to tell the router to emit events on same route refresh. Angular 5.1 introduced the onSameUrlNavigation property on the routers ExtraOptions.

How to show loading indicators in angular router?

Whenever there is a change in angular router, first it will call NavigationStart method as we have subscribed to it. Here we can create a variable which indicates whether we have to show loading indicators such as progress spinner or progress bar. After navigating to the required route, NavigationEnd event will be called.

How to pass dynamic data from one route to another in angular?

The route data can be either static or dynamic. The static data use the Angular route data property, where you can store arbitrary data associated with this specific route. For to pass dynamic data (or an object), we can make use of the history state object. The Routed Component can then retrieve the dynamic data from the history state object.


2 Answers

When listening to router.events the subscribe callback will get a few different types of events, multiple per route change. The ActivationStart is the one that holds the route data. Something like the following helped me:

this.router.events.subscribe(event => {
  if (event instanceof ActivationStart) {
    let data = event.snapshot.data;
    this.titleService.setTitle(data['title'] || '_your_default_title_');
  }
})
like image 83
Radu Avatar answered Oct 12 '22 00:10

Radu


Try instead to use ActivatedRoute from @angular/router

this.activatedRoute.params.subscribe(params => {
    console.log(params['edit']);
    console.log(params['other']);
});
like image 36
Daniel Kucal Avatar answered Oct 11 '22 23:10

Daniel Kucal