0

I need some help in accessing query string from index.html in Angular2 component. I have followed this link but it did not work for me link

my index.html

<!doctype html>
<html>
<head>
  <base href="/">
  <!-- css importd -->
</head>
<body> 
   <app-root>Loading .. </app-root>
</body>
</html>

app.component.ts

...
public constructor(private activatedRoute:ActivateRoute) {}
ngOnInit() {
  this.activateRoute.queryParams.subscribe((params:Params) => {
      console.log(params['code']);
  });
}

And I am invoking url http://localhost:8080/?code=test

5
  • I did not follow you .. how can I set query parameters in template ..? they are coming url. Thanks Commented May 10, 2017 at 5:16
  • Sorry for the mistake, I want to see your HTML code that passes queryparams Commented May 10, 2017 at 5:17
  • Updated HTML code . its very simple file Commented May 10, 2017 at 16:20
  • I am also facing the same issue. Nuthan, Is it resolved for you? Commented Nov 19, 2017 at 5:20
  • No , I am using local storage to pass data to agnular components Commented Nov 20, 2017 at 19:46

2 Answers 2

1

Try this:

constructor(private route: ActivatedRoute) { }

ngOnInit() {
    // get param
    let paramcode = this.route.snapshot.queryParams["code"];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :-

this.activateRoute.queryParams.subscribe(queryParams =>
 console.log(queryParams['code']);
);

Add your html is code :-

<router-outlet></router-outlet>

1 Comment

no luck, i changed template to use <router-outlet></router-outlet>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.