Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 using router.subscribe to watch url change

Tags:

angular

router

I'm using router.event.subscribe @angular/router to watch the url change to execute an if statement though event.subscribe works fine. But my question is how can I avoid repeating my if statement just to show the title on these urls. It's probably something else than router.subscribe but not sure what to use for this.

basically wanted a dynamic title based on the url you are on.

this._router.events.subscribe((event) => {
            console.log('route changed');
            if(this.location.path() == '/1'){
                this.title = 'Title 1';
            } else if(this.location.path() == '/2'){
                this.title = 'Title 2';
            }
        }); 

I don't know if that make sense at all. I could change my route.ts paths to have something like { path: 'Title-1' }and just remove the - by doing .replace() but doing this will give me www.mysite.com/Title-1 but it doesn't look very friendly.

like image 310
nCore Avatar asked Jul 27 '16 13:07

nCore


People also ask

How do I change the URL in an Angular application without reloading the route?

You could use location.go(url) which will basically change your url, without change in route of application. NOTE this could cause other effect like redirect to child route from the current route.

Which Angular package is used to route to URL?

Angular Routinglink To handle the navigation from one view to the next, you use the Angular Router . The Router enables navigation by interpreting a browser URL as an instruction to change the view.

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

What is router and ActivatedRoute?

RouterState and ActivatedRoute are similar to their snapshot counterparts except that they expose all the values as observables, which are great for dealing with values changing over time. Any component instantiated by the router can inject its ActivatedRoute.


1 Answers

Here's my approach which works fine, especially for nested routes:

I use a recursive helper method to grab the deepest available title after a route has changed:

@Component({
  selector: 'app',
  template: `
    <h1>{{title}}</h1>
    <router-outlet></router-outlet>
  `
})
export class AppComponent implements OnInit {
  constructor(private router: Router) {}

  title: string;

  private getDeepestTitle(routeSnapshot: ActivatedRouteSnapshot) {
    var title = routeSnapshot.data ? routeSnapshot.data['title'] : '';
    if (routeSnapshot.firstChild) {
      title = this.getDeepestTitle(routeSnapshot.firstChild) || title;
    }
    return title;
  }

  ngOnInit() {
    this.router.events.subscribe((event) => {
      if (event instanceof NavigationEnd) {
        this.title = this.getDeepestTitle(this.router.routerState.snapshot.root);
      }
    });
  }
}

This is assuming, that you have assigned page titles within the data property of your routes, like this:

{
  path: 'example',
  component: ExampleComponent,
  data: {
    title: 'Some Page'
  }
}
like image 152
Martin Cremer Avatar answered Oct 10 '22 01:10

Martin Cremer