Skip to content

Instantly share code, notes, and snippets.

@isaurssaurav
Created March 25, 2017 15:15
Show Gist options
  • Select an option

  • Save isaurssaurav/f2fb7096923a2ac4e90cc1d99ff4544b to your computer and use it in GitHub Desktop.

Select an option

Save isaurssaurav/f2fb7096923a2ac4e90cc1d99ff4544b to your computer and use it in GitHub Desktop.
working with query param in angular 2
//pass a query param
onNavigate(){
this.router.navigate(['/'],{queryParams:{"analytics":100}});
}
//get a query param in that component, here it is / so it refers to main or home compnent
import { Component, OnInit,OnDestroy } from '@angular/core';
import {Router} from '@angular/router';
import {Subscription} from 'rxjs/Rx';
@compoenent({
})
export class HomeComponent implements OnInit, OnDestroy {
parma:string;
subscription :Subscription;
constructor(private router: Router) {
this.subscription = router.routerState.root.queryParams.subscribe(
(queryParam:any) =>this.parma = queryParam['analytics']
);
}
ngOnInit() { }
ngOnDestroy() {
this.subscription.unsubscribe();
//Called once, before the instance is destroyed.
//Add 'implements OnDestroy' to the class.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment