routing to a component onkeyup angular

So I am trying to implement routing on a search key up. I have my search component as follow

search.component.html
<form>
  <div class="form-group input--compressed">
    <div class="form-group__text text-muted">
      <input id="search" placeholder="Search Object" type="search" (keyup)="search($event.target.value)">
    </div>
  </div>
</form>

Open in new window


search.component.ts like this..
constructor(private router: Router) { }

  ngOnInit() {
  }

search(id: string) {
        this.router.navigate(['/object/' + id]);
}

Open in new window


my route is defined in app.module.ts
const appRoutes: Routes = [
  { path: 'object/:id', component: ObjectSummaryComponent },
  { path: '', component: DisplayErrorComponent }
];

Open in new window


If I just type a value in the search box, it doesn't route to my component. But after typing if I press enter ..it does.  I want to be able to route it on after typing ...or if value selected from list of ajax options for search box.  What am I missing here?
Dev MeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dev MeAuthor Commented:
Any help here please?
0
Dev MeAuthor Commented:
Any update please?
0
Debasis SahaTeam LeadCommented:
Hi,

You can use Angular debounce concept using RxJs operator debounceTime() on a form controls value change method like below -

import {Component}   from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable}  from 'rxjs/Observable';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/throttleTime';
import 'rxjs/add/observable/fromEvent';

@Component({
  selector: 'my-app',
  template: `<input type=text [value]="firstName" [formControl]="firstName">
    <br>{{firstName}}`
})
export class DemoComponent {
  firstName        = 'Name';
  firstNameControl = new FormControl();
  formCtrlSub: Subscription;
  resizeSub:   Subscription;
  ngOnInit() {
    // debounce keystroke events
    this.formCtrlSub = this.firstNameControl.valueChanges
      .debounceTime(1000)
      .subscribe(newValue => this.firstName = newValue);
      .subscribe(e => {
        console.log('resize event', e);
        this.firstName += '*';  // change something to show it worked
      });
  }  
} 

Open in new window


Hope this will help you..
1
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dev MeAuthor Commented:
Not sure if I followed Debasis, my issue is that 'this.router.navigate' doesn't navigate on keyUp.

 this.router.navigate(['/object/' + id]);

Open in new window

0
Dev MeAuthor Commented:
I haven't received any update for few days despite reporting the question multiple times.
0
Debasis SahaTeam LeadCommented:
You can use the below code

window.location.replace('/object/' + id);

in place of

this.router.navigate(['...']);
0
Dev MeAuthor Commented:
ok thanks.  So this.router.navigate(['...']) is not valid?
0
Debasis SahaTeam LeadCommented:
Is this work for you? router.navigate is valid but as per your scenario, it does not work.
0
Dev MeAuthor Commented:
your solution is working. Want to understand why router.navigate would not work for my scenario.?
0
Debasis SahaTeam LeadCommented:
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.