0

I want to check my URL that has exactly a specific format. I have this URL

www.example.com?rt=12365

I want to check if income URL has below format

?rt= number

then do something. I use the router like my below code:

const queryParams = router.url

but I don't know how to check it. I'm new to typescript and angular, please help me

1

2 Answers 2

3

Try this

 constructor(private route: ActivatedRoute) {

 }

 if (this.route.snapshot.queryParams['rt']) {
      if ( !isNaN(this.route.snapshot.queryParams['rt']) && angular.isNumber(this.route.snapshot.queryParams['rt'])) 
       {
         }
 }

Hope useful for you

6
  • How do I check that there is a number in front of the rt and nothing more؟
    – heliya rb
    Jan 8, 2020 at 8:57
  • @heliyarb also might be worth checking out Lodash, quite a nice library with a lot of useful functions: lodash.com/docs/4.17.15#isNumber Jan 8, 2020 at 9:41
  • what is angular in this code? I get Cannot find name 'angular' error
    – heliya rb
    Jan 8, 2020 at 9:43
  • import {isNumeric} from "rxjs/util/isNumeric" var isNumber = isNumeric(this.route.snapshot.queryParams['rt']) cehck this
    – A.khalifa
    Jan 8, 2020 at 9:48
  • add it but still has an error. I think it's for angular.js. I use angular 8.
    – heliya rb
    Jan 8, 2020 at 9:56
2

You can use queryParams

constructor(private route: ActivatedRoute) {

}
ngOnInit() {
    this.route.queryParams.subscribe(params => {
        console.log(params.rt);
        if(!isNaN(params.rt)){
            console.log("URL Match");
        }else{
            console.log("URL does not Match");
        }
    });
}
4
  • this code just gets rl to me. I need to check that if URL exactly has "rt=number" format
    – heliya rb
    Jan 8, 2020 at 9:39
  • you can check if(params.rt) for value of rt is exist or no Jan 8, 2020 at 9:42
  • should I check like this: has("rt")?
    – heliya rb
    Jan 8, 2020 at 9:49
  • so how to check that front of rt is number?
    – heliya rb
    Jan 8, 2020 at 9:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.