Search code examples
typescriptangular2-routing

Retrieve hash fragment from url with Angular2


Given this url structure (over which I have no control), how can I retrieve the hash fragment using Angular2?

http://your-redirect-uri#access_token=ACCESS-TOKEN

My router does route to the correct component, but everything after oauth get scrapped and I can't find the hash fragment in request.params or location.path. Doomed??

Router config:

@RouteConfig([
{path: '/welcome', name: 'Welcome', component: WelcomeComponent, useAsDefault: true},
{path: '/landing/oauth', name: 'Landing', component: LandingComponent}  // this one

])


Solution

  • For those still looking :

    import { ActivatedRoute } from '@angular/router';
    
    export class MyComponent {
    
      constructor(
        private route: ActivatedRoute,
      ) { }
    
      myfunction(){
        this.route.fragment.subscribe((fragment: string) => {
            console.log("My hash fragment is here => ", fragment)
        })
      }
    }