9

I want to have back-to-the-previous page.

Previous Page: job.html Current Page: jobDetail.html

As per instructions, I have added import { Location } from '@angular/common'; to the jobDetail.component.ts file at the top followed by

export class MyDetailComponent implements OnInit {
constructor(private location: Location) {}
    ngOnInit() {
        this.location.subscribe(x => console.log(x));
    }
}

I have a html code in jobDetail.html but don't know how to proceed further. How do I add a previous button correctly. There's no easy tutorial for newbies like me.

<a routerLink="">Back</a>
Elaine Byene
  • 3,868
  • 12
  • 50
  • 96

1 Answers1

21

This worked as posted by Hinrich:

import { Location } from '@angular/common';
// more imports here...

@Component({
  // component's declarations here
})
export class MyComponent {

  constructor(private location: Location) { } // inject Location into class constructor

  cancel() {
    this.location.back(); // <-- go back to previous location on cancel
  }
}

HTML

<a (click)="cancel()">Back</a>
Elaine Byene
  • 3,868
  • 12
  • 50
  • 96