1

How can I get the value of param2 based on name in angular?

http://localhost:4200/home#parma1=value1&param2=value2&param3=value3

Tried Below:

constructor(
private router: Router,
private route: ActivatedRoute) { }


ngOnInit(): void {
this.route.queryParams
  .subscribe(params => {
    console.log(params);    // Out put : {} 
  }
);
console.log(this.router.url); // Output : /home#parma1=value1&param2=value2&param3=value3

}

Is there any standard approach to get the parameters when parameters separated with # instead of ? ?

Yared
  • 2,206
  • 1
  • 21
  • 30
Sai Ram
  • 63
  • 1
  • 8

4 Answers4

1

since activatedRoute doesn't recognize params if wasn't seperated by ? based on your example. Just use the traditional URLSearchParams

by this, you can get the value of param2

const params = new URLSearchParams("http://localhost:4200/home#parma1=value1&param2=value2&param3=value3");

const param2 = params.get("param2");
console.log(param2)
Stacks Queue
  • 848
  • 7
  • 18
0

This should work. You can follow the https://angular.io/guide/router#activated-route-in-action

this.route.queryParams.subscribe(params => { this.name = params['name']; });

bhathiya.m
  • 215
  • 2
  • 12
0

try to use queryParamMap

ngOnInit(): void {
this.route.queryParamMap
  .subscribe(params => {
    console.log(params);
  }
)

or use

let param1 = this.route.snapshot.queryParamMap.get('param1')
Chady BAGHDADI
  • 203
  • 2
  • 7
0

Those after "#" called fragment, and the way to get it is

this.activatedRoute.fragment
  .subscribe(frgmt => {
    console.log(frgmt);    // Output (type string): parma1=value1&param2=value2&param3=value3' 
  }
);

if you want param but don't want to use question mark, then use matrix. Something like this: /app;param1=1;param2=2;param3=3

Jimmy
  • 1,276
  • 7
  • 8