Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
Angular Get last part of url

I want to get the last part of my url for example from /item/:id/edit I want to get edit or /edit.

I found this Question here but this doesnt work because I want to run this Code in the parent component and the whole url I get from this Code is /item/:id.

How can I get the last part from it?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I have the solution for getting the last part of URL

url = '/item/:id/edit'
url.split('/').pop();

Explain:

  1. .split('/') means split the string which is separated by '/' to an array of substrings
  2. .pop() means removes the last element of an array, and returns that element
  3. .split('/').pop() means split the string which is separated by '/' to an array of substrings then removes the last element of an array and return that element

I hope this help

over 4 years ago · Santiago Trujillo Report

0

You can use ActivatedRoute interface, it contains the router state tree within the angular app’s memory.

The parent property represents the parent of the route in the router state tree, while the snapshot property is the current snapshot of the route and url is an observable of the URL segments and it matched by this route.

Step 1: Import ActivatedRoute

    import { ActivatedRoute } from '@angular/router';

Step 2: Inject ActivatedRoute in constructor.

    constructor(private route: ActivatedRoute) { }

Step 3: Call it in Oninit lifecycle hook

      constructor(private route: ActivatedRoute){}
        
           ngOnInit() {
              this.route.parent.snapshot.url[2].path;
           }
over 4 years ago · Santiago Trujillo Report

0

Since you want to get the URL part being in a parent component, you have to wait until the navigation occurs.

Get the last URL part from a parent component

router.events
  .pipe(
    filter(event => event instanceof NavigationEnd),
    map((event: NavigationEnd) => event.url)
  )
  .subscribe(url => {
    const lastUrlSegment = url.split('?')[0].split('/').pop()
  });

Where router is an injected Angular Router service. Here url.split('?')[0] returns the URL part without query parameters in case they exist, then split('/').pop() returns the last URL segment without the / character.

In case you'd like to

Get the last URL part from the child component

You could do that synchronously

const lastUrlSegment = router.url.split('?')[0].split('/').pop()

Where router is an injected Angular Router service. Pay attention, that under the child component I mean the one, rendered by the last URL segment we're looking for.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!