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.

compareWith not triggered when list of items is updated

See original GitHub issue

Describe the bug We used to have an ng-select like this:

<ng-select [items]="items" groupBy="group" bindLabel="value" formControlName="item"></ng-select>

The list of items looked like this: [{code: 'A', value: 'description', group: 'some group'}]. The list was provided by a parent component and was async so you could start with an empty list. When the model already had a selected item when you opened the page it could take a small amount of time until the items were finally loaded before it would show the selected item in the input field. This behaved as expected.

We recently changed this to:

<ng-select [items]="items" groupBy="group" bindLabel="description" bindValue="item" [compareWith]="isMatch" formControlName="item"></ng-select>

The list of items looks like this now: [{description: 'alternate description', item: {code: 'A', value: 'description'}, group: 'some group'}].

isMatch looks like:

isMatch(toCompare, selected) { return toCompare.code === selected.code; }

When the model already has a selected item and the data is already available or fast enough there are no issues. But when the list of items is empty at first and then gets updated very quickly afterwards the ng-select input stays empty, even though the selected item is in the list. I checked to see if compareWith was called at all when the items were updated. It wasn’t. The items list is a new array, not a modified one. The items are shown in the dropdown, it’s just that the item that’s selected is not shown in the input field.

Reproducible example Not available

To Reproduce Have an ng-select with bindValue and compareWith where bindValue is a nested object inside an item. Start with a selected item in the model. Items should be an empty list to start with, then update with a list with all the items, including one that contains the selected item. The input field stays empty, most likely because compareWith isn’t executed to check if the selected item matches an item in the new list.

Expected behavior I’d expect the selected item to be shown in the input when the empty items list is replaced with the loaded items.

Desktop (please complete the following information):

  • OS: Arch Linux
  • Browser chrome 81.0.4044.92
  • ng-select version 4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
lbsncommented, Apr 30, 2021

Please reopen this issue. I have a very similar (if not the exact same) use case. Here a reproducible example: not-working-compare-with

From what I can understand, the problem seems to be that writeValue() on NgSelectComponen doesn’t get called when items change and on the first call items is still empty.

0reactions
davidecampellocommented, May 18, 2022

I’m having the same issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular compareWith not working in non-material select
The mat-select correctly triggers the comparing function, whereas the plain select does not. Is this a bug in angular or am I missing...
Read more >
angular select does not load my selected option | Nerd For Tech
The importance of the compare function compareWith() , a property used with the form control for comparing option values with selected values.
Read more >
Ion-select compareWith NOT firing - Ionic Forum
I have a data id from a mondo db, where I store just the _id of the item in the data record for...
Read more >
Documentation - SolidJS · Reactive Javascript Library
The first execution of the effect function is not immediate; it's scheduled to run ... would have been called twice, once when updating...
Read more >
Reference guide for expression functions - Azure Logic Apps
item, If this function appears inside a repeating action over an array, return the current item in the array during the action's current...
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