Created
March 25, 2017 15:15
-
-
Save isaurssaurav/f2fb7096923a2ac4e90cc1d99ff4544b to your computer and use it in GitHub Desktop.
working with query param in angular 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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