Angular 2 ng bootstrap typehead pass additional parameter

前端 未结 1 1079
耶瑟儿~
耶瑟儿~ 2021-01-14 03:46

How to pass form array index to getCities function in ng-bootstrap typehead including current input text. Consider 3 is form array index.

ad

相关标签:
1条回答
  • 2021-01-14 04:23

    It sounds like you are needing to pass an additional parameter to the ngbTypeahead function (be it an index parameter or otherwise).

    While the documentation ( https://ng-bootstrap.github.io/#/components/typeahead/api ) does not provide for passing parameters, you can implement a "factory" method that returns an appropriate function with the index (or whatever) parameter passed in.

    address.component.html

        <input name="city" 
            type="text" 
            id="city"
            formControlName="city"
            [ngbTypeahead]="searchFunctionFactory($index)" >
    

    address.component.ts

        //A function that returns a "search" function for our ngbTypeahead w/ "preloaded" parameters
        public searchFunctionFactory($index: any): (text: Observable<string>) => Observable<any[]> {
    
    
            //Create a function that considers the specified $index parameter value
            let getCities = (text$: Observable<string>) => 
                text$
                    .debounceTime(300)
                    .distinctUntilChanged()
                    .switchMap( query => {
    
                        //some logic involving $index here
                        //...
    
                        //query.length < 2 ? [] : this.apiService.getCities(query).catch(() => {
                        //return Observable.of([]);
                    });
    
            //Return that "custom" function, which will in turn be called by the ngbTypescript component
            return getCities;
        }
    
    0 讨论(0)
提交回复
热议问题