question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression: ngModel doesn't work with search function bind(this) ([ngbTypeahead]="search.bind(this)")

See original GitHub issue

Bug description:

In case if I use search.bind(this) ngModel isn’t working, data isn’t set to model

html:

<input
 id="typeahead-basic"
  type="text"
   [ngbTypeahead]="search.bind(this)"
   class="form-control"
   [(ngModel)]="model"/>

ts:

search(text$: Observable<string>) {
    return text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(term => term.length < 2 ? []
        : this.states.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
    )
  }

This was working on v/7.0.0

Link to minimally-working StackBlitz that reproduces the issue:

https://angular-dd3z93.stackblitz.io

Versions of Angular, ng-bootstrap and Bootstrap:

Angular: 11.2.7

ng-bootstrap: 9.1.0

Bootstrap:

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:26

github_iconTop GitHub Comments

4reactions
lschuftcommented, Aug 31, 2021

@KyleThenTR To keep things short, my scenario consisted on a table with rows rendered via a ngFor. Each row had a typeahead. I passed a parameter to each typeahead to allow them to know which row they corresponded to. To avoid passing a parameter, I simply encapsulated each row into its own Component. That way, the parameter became one of the Input properties of this new Component, so there was no need to use a parameter in the typeahead function; simple grab that information from the Component itself.

3reactions
icyerasorcommented, Jul 22, 2022

The proposed workaround is to use a pure pipe. It’s really suuuuper easy:

  1. create the pure pipe
import { Pipe, PipeTransform } from '@angular/core';


@Pipe({
  name: 'applyPure',
  pure: true, // thats important
})
export class ApplyPurePipe implements PipeTransform {
  public transform(templateValue: any, fnReference: Function, ...fnArguments: any[]): any {
    // join the inputs from both sides of the function ref
    fnArguments.unshift(templateValue);

    return fnReference.apply(null, fnArguments);
  }
}
  1. use it where you need it like this:
[ngbTypeahead]="'PARAM' | applyPure: typeaheadFunction"

with your typeaheadFunction being something along the lines of


  typeaheadFunction: (param: string) => OperatorFunction<string, readonly Result[]> = (
    param: string,
  ) => (text$: Observable<string>) => {
    return text$.pipe(
      debounceTime(300),
      distinctUntilChanged(),
      switchMap((textTyped) =>
        typedText.length < 2 ? [] : this.searchService.search(param, textTyped),
      ),
    );
  };

really not sure if I’m a fan 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template parse errors: Can't bind to 'ngbTypeahead' since it ...
Show activity on this post. I get the error "Template parse errors: Can't bind to 'ngbTypeahead' since it isn't a known property of...
Read more >
Using the ng-BootStrap TypeAhead Control with Dynamic Data
The control is data bound to the Artist in the model (the [(ngModel) attribute) and when the type ahead control expects to search...
Read more >
angular/angular - Gitter
I am trying to include topology.feature but I am not able to display basic map. I read through the documentation but not able...
Read more >
Angular Ngbtypeahead Doesn't Reset Value Of Input On Blur
installing ng bootstrap ng add ngbootstrap ngbtypeahead angular ... clear input on blur Regression: ngModel doesn't work with search function bindthis.
Read more >
Angular error Can't bind to 'ngModel' since it isn't a ... - YouTube
Learn Angular in Mumbai offline http://stepbystepschools.net/For more such videos visit http://www.questpond.comSee our other Step by Step ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found