oferh / ng2-completer

Angular 2 autocomplete component
http://oferh.github.io/ng2-completer/
MIT License
347 stars 172 forks source link

Multiple Parameters in Remote Data URL #351

Open ctyrrell70 opened 6 years ago

ctyrrell70 commented 6 years ago

Hi,

I want to pass in multiple arguments to a URL, for example "http://www.myapi.com?arg1={arg1}&arg2={arg2}".

I know there is a way to pass one term in through the urlFormater, but is there a way to make this work if we have some other field on the form like a toggle that affects the search query?

Thanks!

oferh commented 6 years ago

when using url formatter you can change the URL in any way that you want see the example in the demo

this.dataRemote2.urlFormater(term => {
            return `https://api.themoviedb.org/3/search/movie?api_key=36bf560f8967672b5e428038340f0065&language=en-US&query=${term}&page=1&include_adult=false`;
        });
ctyrrell70 commented 6 years ago

I actually meant if I had another variable that was dynamic in nature, like a dropdown select that changes the value of a parameter going into the API call.

With ulrFormater you can only pass in one variable to the string, which is "term" in your example. But what if I wanted another variable to be passed in that might change if the user selects a specific value in a dropdown? Like if urlFormater could except multiple arguments, or an array of arguments and plug them in accordingly for multiple dynamically changing variables to be plugged in as parameters.

oferh commented 6 years ago

you can do something like this:


private myOtherValue: string;

constructor() {
    this.dataRemote2.urlFormater(term => {
        return `https://example.com/search?term=${term}&myOtherParam=${this.myOtherValue}`;
     });
}

public onValueSelected(newValue: string) {
    this.myOtherValue = newValue;
}