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.

Support for setting option's control value to be a property of object and option's display value to be another property of the object

See original GitHub issue

I know that if we want the option’s control value (what is saved in the form) to be different than the option’s display value (what is displayed in the text field), we’ll need to set the displayWith property on our autocomplete element. as explained here:

https://material.angular.io/components/autocomplete/overview#setting-separate-control-and-display-values

Suppose we have a user object like { id: 1, name: 'Abc' }.

What if we want the option’s control value (what is saved in the form) to be user.id and option’s display value (what is displayed in the actual text field) to be user.name?

In my case I want to display a list of users in autocomplete options and when user selects a user, I want to display the selected user name on the user screen but when the form is submitted, I just want user.id to be posted to the server not user.name and not complete user object.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
naveedahmed1commented, Jan 28, 2019

Yes, I know that’s possible, but may we can have something like displayWith that allow specifying which value should be saved for form control, to avoid this step of assigning the entire object as the value and then only taking the specific property of object we want before firing off the request, may be something saveValue.

This would be really helpful for apps where we have dynamic forms, and where we just pass the model and the post url and rest is handles by the dynamic form component.

2reactions
mrmokwacommented, Feb 12, 2019

+1

This issue have already been asked before a couple of times, but didn’t get much attention (the first reference I saw was about to get 2 years old).

For me this comment from @kdubious, summarize the problem: https://github.com/angular/material2/issues/8436#issuecomment-353203199

I’m getting tripped up on implementing what I think is a fairly standard use case for a combobox (autocomplete).

For now, all we have is this jerry-rig, which solves the problem. https://stackblitz.com/edit/angular-autocomplete-array-with-formcontrol-id-1?file=app%2Fautocomplete-display-example.ts

I would recommend avoiding use Autocomplete for this scenario, and use Selector componente instead. … It’s what I had to do unfortunatelly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding objects, methods, properties, and events (VBA)
A property is an attribute of an object that defines one of the object's characteristics, such as size, color, or screen location, or...
Read more >
Object.defineProperty() - JavaScript - MDN Web Docs
When the property already exists, Object.defineProperty() attempts to modify the property according to the values in the descriptor and the ...
Read more >
Google Visualization API Reference | Charts
The formatted value is typically used by visualizations to display value labels. For example the formatted value can appear as a label text...
Read more >
The "options" binding - Knockout.js
Similar to optionsText , you can also pass an additional parameter called optionsValue to specify which of the objects' properties should be used...
Read more >
How to set value to form control in Reactive Forms in Angular
Setting or Updating of Reactive Forms Form Control values can be done using both patchValue and setValue. However, it might be better to...
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