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.

setValue() does not work for nb-select component

See original GitHub issue

Issue type

I’m submitting a … (check one with “x”)

  • bug report
  • feature request

Issue description

Current behavior:

Setting the value of nb-select programmatically sets the value on Reactive Form control but nb-select does not display it.

Expected behavior:

nb-select displays the value set programmatically.

Steps to reproduce:

  • Create Reactive Form
  • Add nb-select to the form
  • Set object value to the -nb-select

Related code:

<form [formGroup]="form">
  <nb-select formControlName="user">
    <nb-option *ngFor="let u of users" [value]="u">{{u.name}}</nb-option>
  </nb-select>
</form>

this.form = this.fb.group({
  user: ['']
});
this.form.get('user').setValue({ name: 'Maihan', age: 55, gender: 'Male' });
this.form.controls['user'].patchValue(
  { name: 'Maihan', age: 55, gender: 'Male' }
)

StackBlitz StackBlitz Seed Project

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
vitmaraliagacommented, Sep 2, 2019

Try with this setTimeout(): function.

setTimeout(() => {
              this.myForm.get('control').patchValue('new-value');
            }, 0);

4reactions
Tibingcommented, Jun 27, 2019

Hi @maihannijat, from my point of view, nb-select works as expected. Regarding the setValue(), as I see it, you’re providing values for nb-option’s as references to objects from users array. And then, passing the different object with different reference to the setValue() method. nb-select internally compares items by reference using plain === operator. So, in your case, please, pass user’s id’s as values to nb-option’s like that one:

 <nb-option *ngFor="let u of users" [value]="u.id">{{u.name}}</nb-option>

and then call setValue() with the appropriate user.id.

Or just pass the same user object like so:

this.form.get('user').setValue(this.users[0]);

Regarding the reset method. As I see it, it’s a private method of nb-select. And when it’s used internally, nb-select also calls markForCheck() after the call. So, you have to mark the component as dirty after calling that method. By the way, I don’t think you have to use it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Formbuilder setvalue() is not working as expected when used ...
I expect the corresponding dropdown option to be selected when I pass the value to the setValue() . Instead, I get a blank...
Read more >
Select setValue does not work as documented and as expected
The setValue method of the Select component does what the documentation says and sets the value of the Select to the given string...
Read more >
setValue on Textarea/Select Components from Event Handler ...
Hey guys, To explain the problem I'm seeing, I'm using the retool on-premise version 2.80.16 and I am attaching event handers to set...
Read more >
Dropdownlist reactive form setValue not working - EJ 2 Forums
I'm having a problem assigning a value to a reactive form control dropdownlist. Value is not set. Form definition: private defineForm() {.
Read more >
API - React Select
Even when commonProps are not listed in the prop types below, a custom component will still have access to them. StateManager Props. The...
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