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.

Typeahead - ngModel vs SelectedItem

See original GitHub issue

Currently, if you have <input [(ngModel)]="model" [ngbTypeahead]="fn">, then the model property is a string while you are typing in the input and an Object once you select a value (depending on the type of objects you return from your typeahead).

I think it would be good if we separate these two somehow. This is only my opinion/proposition, but it seems that ngModel could be bound the selected object from the typeahead whereas we could receive the changes to the input in another event if necessary.

I would be interested to know what others think. Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
dimitriy-kcommented, Jun 19, 2017

selectItem does not works for me.

I have as dropdown model {type: "query", value: "some test value"}

   selectItem(event: any) {
    const item = event.item;
    if (item.type === 'query') {
      this.form.query = item.value;
    }
  } 

html

  <input
          class="form-control"
          [ngbTypeahead]="query"
          [resultTemplate]="combinedTpl"
          [inputFormatter]="queryFormatter"
          (selectItem)="selectItem($event)"
          type="text"
          id="query"
          name="query"
          [(ngModel)]="form.query"
          autocomplete="off">

So, when you type, the model will be a string. When you select an item, it will run selectItem function and you expect it should set form.query to the string item.value, but instead of that form.query will be whole object {type: "query", value: "some test value"}. I think it is because of this code:

    if (!defaultPrevented) {
      this.writeValue(result);
      this._onChange(result);
    }

when closing the drowpdown, it will set object as a model.

But, when you use event.preventDefault();, it will set correctly this.form.query, but input value will be cleared/empty, so next time you focus on input, also the model will be cleared.

3reactions
pkozlowski-opensourcecommented, Nov 22, 2016

I think that we are going to leave it as-is due to the following reasons:

  • you can use the [editable]="false" option to skip propagation to the model values entered by the user
  • you can use the selectItem event to have an event fired when users make a selection - in this sense separation already exists.

I’m going to close this one as I believe that we’ve got all the necessary mechanisms in place. Happy to re-consider if you think that we can do better and:

  • have a concrete use-case
  • have a more detailed proposal
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I capture NgbTypeahead SelectedItemEvent?
I am unable to bind selected item event with typeahead control. ... protocol and separate out [(ngModel)] into [ngModel] and (ngModelChange).
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 >
UI Bootstrap - AngularUI
This repository contains a set of native AngularJS directives based on Bootstrap's markup and CSS. As a result no dependency on jQuery or...
Read more >
Value Binding - DropDownList - Kendo UI for Angular - Telerik
When the selected item is an object, always specify valueField . ... If the value which is assigned through the [value] or [(ngModel)]...
Read more >
Angular Material - Autocomplete - Tutorialspoint
<md-autocomplete> can be used to provide search results from local or remote data sources. md-autocomplete caches ... A model to bind the selected...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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