0

Hi I have an input search section where a user types a name and it dynamically sends a HTTP get request with the name. Now I need to take in two arguments in the input (first name, and last name) and as user types it in, dynamically seperate that as seperate variable and send a GET request like this ?first_name=${first name}&last_name=${last name}

Right now, when a user types, I can make it set to either first name or last name, but don't know how I can seperate it..

my input looks like this:

  <label for="i2" class="control-label">Search Employees</label>
  <input name="term"
        [(ngModel)]="term"
        (keyup)="searchTerm()"
        type="text"
        class="form-control"
        id="i2" />

in the component:

 ngOnInit(): void {
    this.busy = this._dataService.searchTerm(this.term)
      .subscribe(employees => this.employees = employees);
  }

  searchTerm() {
    this._dataService.searchTerm(this.term)
    .subscribe(employees => this.employees = employees)
  }

and I have a search service that uses the term to make an api GET request:

  searchTerm(searchString:any, searchString1:any, type="last_name", type1="first_name") : Observable<IEmployee[]> {
    let headers = new Headers();
    this.createAuthorizationHeader(headers);

    this.url = `someUrl?${type}=${searchString}&${type1}=${searchString1}`;

    this.numberUrl = `someUrl?employee_number=${searchString}`;


    if (isNaN(searchString) || searchString < 1)
        return this._http.get((this.url), {headers})
            .map((resp: Response) => <IEmployee[]>resp.json())
        else {
          return this._http.get(this.numberUrl, {headers})
            .map((resp: Response) => <IEmployee[]>resp.json())
        }
    }

I tried splitting and using regex to detect a comma and then separate the searchString, but i get can't use split or regex on undefined..

Is it even possible to dynamically seperate a user input(an ngModel) into two separate values??

2
  • Did you try doing the split inside component searchTerm()?
    – John Siu
    Mar 22, 2017 at 21:15
  • 1
    I figured out a way, you can use ngChange on the term and change user inputs to an array. Then you can just reference the array elements in the searchTerm function.
    – Kimmiekim
    Mar 23, 2017 at 14:44

0

Your Answer

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