0

I was able to get the authkey value using route paramMap, but how do we get the url. For example, I want to check if URL contains resetpassword text as you can see on the link below. How do we do that in Angular?

How do we get URL or if URL contains certain text on page load? Check if URL contains "resetpassword" text.

Code

ngOnInit(): void {
  this.router.paramMap.subscribe((paramMap) => {
    console.log("paramMap" , paramMap)
    this.code = paramMap.get('authkey');
    if (this.code) {
      this.code = paramMap.get('authkey').replace('authkey:', '');
      this.getEmail(this.code);
    }
  });
}

Example URL

http://localhost:4200/#/login/resetpassword/authkey:6762b362-1a62-4077-8ae4-78b381REX7JEXE4a8f30

URL in address bar

Roy
  • 7,811
  • 4
  • 24
  • 47
  • 2
    Does this answer your question? [Get current url in Angular](https://stackoverflow.com/questions/45184969/get-current-url-in-angular) – A_A Jul 29 '21 at 12:23
  • No , cause I also wanted to check if url contains specific string –  Jul 29 '21 at 12:24
  • cant really find a solution –  Jul 29 '21 at 12:32
  • Ok. 2 approaches: You can get the url as described in the other answer, and then google for "js check if string contains substring" -> https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript. Or you google directly "js check if url contains text" -> https://stackoverflow.com/questions/4597050/how-to-check-if-the-url-contains-a-given-string – A_A Jul 29 '21 at 12:35
  • let subscring ="resetpassword" console.log("thisss" ,this.router.url.includes(subscring)) –  Jul 29 '21 at 12:37
  • Property 'includes' does not exist on type 'Observable'.ts(2339) any –  Jul 29 '21 at 12:38

1 Answers1

1

include is a function that can use to find a string value in a string

if(this.router.url.includes('resetpassword')){
  //Implement code here if exists 
}else{
 //Implement code here if not exists 
}