Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if router.url contains specific string?

Tags:

angular

I have this :

   if (this.router.url === '/test/sort') {

                    this.active = 0;
                }

Problem is that sometimes url will be test/sort?procesId=11 and than it will not enter in if statement. Any suggestion how can i do that?

like image 741
None Avatar asked Aug 29 '17 08:08

None


People also ask

How do I check if a URL contains a string?

Use indexOf() to Check if URL Contains a String When a URL contains a string, you can check for the string's existence using the indexOf method from String. prototype. indexOf() . Therefore, the argument of indexOf should be your search string.

How do you check if the URL contains a given string in angular?

Method String. includes(searchString: string, position?: number): boolean - Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false.

What does URL indexOf do?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

How do I find my router's URL?

Find Your Router's IP Address Most routers use an address of 192.168. 1.1, but that's not always the case, so you may first want to confirm the address of your router. To find your router's IP address, type cmd in the Windows search bar open the Command Prompt. Type ipconfig and run the command.


2 Answers

If you want something basic that works:

if (this.router.url.indexOf('/test/sort') > -1) {
  this.active = 0;
}
like image 141
Carsten Avatar answered Sep 22 '22 18:09

Carsten


You can also use:

if (this.router.url.includes('/test/sort')) 
{  
     this.active = 0; 
}

Method String.includes(searchString: string, position?: number): boolean - Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false.

like image 39
Marko Letic Avatar answered Sep 21 '22 18:09

Marko Letic