2

I have a search bar in a header component.

Beneath that, I have a "router-outlet" in that same component.

The search bar (input txtfield), once enter is pressed, needs to send the search string (event.target.value) to the component that resides within the router-outlet beneath it so that it can run a method to return the results.

I have no clue what the best way is to achieve this.

UPDATED with code..

app.component.html:

<div class="white-container">
     <input name="searchStr" [(ngModel)]="searchStr" (keyup.enter)="searchCourse($event)">
</div>

<router-outlet></router-outlet>

app.component.ts:

import { Component, OnInit } from '@angular/core';
import { CourseService } from './services/courses.service';
import { Course } from './Course';

@Component({ 
   selector: 'my-app',
   templateUrl: 'app.component.html',
   providers: [CourseService]
})

export class AppComponent implements OnInit { 

    constructor(private _courseService: CourseService) {

    }

    searchCourse(event) {
       // the user search string here...
    }

}

/course-listings/course-listings.component.ts:

import { Component, OnInit } from '@angular/core';
import { CourseService } from './services/courses.service';
import { Course } from './Course';

@Component({ 
  selector: 'app-course-listings',
  templateUrl: './course-listings.component.html',
  styleUrls: ['./course-listings.component.css'],
  providers: [CourseService]
})

export class AppComponent implements OnInit { 

    course: Course[];

    constructor(private _courseService: CourseService) {

    }


    searchCourse(evt) {

        // This works once it's fired... 

        this._courseService.findCourse(evt)
            .subscribe(courses => { 
                this.course = courses; 
        });

    }


}

/services/courses.service.ts:

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()

export class CourseService {


    constructor(private _http:Http) {

    }

    getCourses(search) {
        return this._http.get('/api/v1/courses/'+search)
            .map(res => res.json());
    }

}

FIX FOUND

Günter Zöchbauer was correct. I used a service w/ subscribe and observables to do it. Thanks.

Gary Simon
  • 373
  • 3
  • 13
  • 1
    https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service – Günter Zöchbauer Nov 15 '16 at 06:31
  • Can you please be more specific and add your code here or in plunkr? That would be better – The Hungry Dictator Nov 15 '16 at 07:14
  • I updated the original post with the code. Gunter is eluding to using services, which honestly, I'm not exactly sure how to integrate what I need based on the link provided... and the individual below is suggesting adding a router event listener. I'm not sure which. Hopefully someone can help, thanks. – Gary Simon Nov 15 '16 at 14:58
  • I've managed to get a service integrated so that it passes the user input from the main app.component, to a {{ searchStr }} property in the course-listings component view. What I don't understand, is how I can call the method needed to display the results **when** the subscription is called (or rather, when a new search is performed). – Gary Simon Nov 15 '16 at 20:25
  • I did it! I realized I had to put the code inside of my previous function "searchStr" inside of the subscription within the constructor: this.subscription = this._interactionService.searchStr$.subscribe( //listings stuff here ) – Gary Simon Nov 15 '16 at 20:30
  • Could you please post the full answer please ? – fallais Apr 03 '19 at 09:12

1 Answers1

0

An event.subscriber would be required in the constructor to pass to router-outlet. Similar to the answer in this Angular 2 router event listener.

So, once the click is done, the subscriber event will be executed based on on the navigationend event, then the value can be accessed.

Community
  • 1
  • 1
GeAlBe
  • 11
  • 2
  • Thanks for commenting. Still a bit confused on how to implement this. Could you take a look at my updated answer with the code so I can have a more specific idea of how to integrate your approach? Thanks. – Gary Simon Nov 15 '16 at 18:21