5

I am working on a SpringBoot and Angular 5 web app, where I have a search form in the header:

<div class="searchDiv float-right">
    <form (ngSubmit)="onsubmit()" >
      <input class="searchBtn searchBtnHeader" type="text" role="search" placeholder="Search..." title="search field">
      <button class="fa fa-search align-middle searchBtn searchIcone" type="submit" [routerLink]="['/search']"></button>
    </form>
</div>

How can I do redirection to a more detailed search page(component) in Angular 5?

1
  • Do it in the controller: in a submit handler for the form, do whatever you need to with the form and then router.navigate() to your destination
    – msanford
    Apr 30, 2018 at 12:45

2 Answers 2

18

In your onSubmit() method, you can add a programmatic redirection, like this

constructor(private router: Router) { }

onSubmit() {
  // some stuff
  this.router.navigate(['/search']);
}

So you don't need anymore the [routerLink] attribute in your submit button.

For a complete guide on Angular 5 redirection ,take a look at this detailled tutorial

1
  • Hi, this will get moderated as a link-only answer: can you please take some of the information out of the tutorial and paste it inline (like the other answer has)?
    – msanford
    Apr 30, 2018 at 12:46
7

Programatically route from onsubmit() method

In Class

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

...
constructor(private router: Router) {}
...
onsubmit() {
      this.router.navigate(['/search']);
}

Your Answer

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

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