Skip to content

Select-Option: SelectedValue not working when changing DataSource #13884

Closed

Description

@rongchaua

Hi all,
i'm having a problem with the data binding of SelectedValue of select-option(ComboBox) when I change the DataSource during runtime.

I have a select-option and bind it with a list. The content of the list will be changed during run-time (cleared and inserted with new item). At the beginning I fill the list with these data

mockData1: ListItem[] = [
{ V: "CH", D: "Schweiz" },
{ V: "DE", D: "Deutschland" },
{ V: "VN", D: "Vietnam" }
];

In the UI, I select DE-Deutschland which means that my current selected-value is DE.
Then I clear the list and fill it with other data

mockData2: ListItem[] = [
{ V: "CH", D: "Schweiz" },
{ V: "DE", D: "Deutschland" },
{ V: "VN", D: "Vietnam" },
{ V: "US", D: "USA" }
];

Problem: My current selected-value is still DE, but in the UI nothing is selected.
Question: What should I do so that the databinding still works after I refresh the datasource? Thank you.

I create a sample here https://github.com/rongchaua/angularselect
Or in short version

export class AppComponent implements OnInit {

  items: ListItem[] = [];
  mockData1: ListItem[] = [
    { V: "CH", D: "Schweiz" },
    { V: "DE", D: "Deutschland" },
    { V: "VN", D: "Vietnam" }
  ];
  mockData2: ListItem[] = [
    { V: "CH", D: "Schweiz" },
    { V: "DE", D: "Deutschland" },
    { V: "VN", D: "Vietnam" },
    { V: "US", D: "USA" }
  ];
  model: string;

  constructor(
    private http: Http
  ) {

  }

  load1() {
    console.log("list 1 loaded");
    this.items.length = 0;
    for (let item of this.mockData1) {
      this.items.push(item);
    }
  }

  load2() {
    console.log("list 2 loaded");
    this.items.length = 0;
    for (let item of this.mockData2) {
      this.items.push(item);
    }
  }

  ngOnInit(): void {
    this.load1();
  }
}
<div class="row top-buffer">
    <label class="col-md-3" for="">Lala</label>
    <div class="col-md-9">
        <select class="form-control" name="" [(ngModel)]="model">
                <option *ngFor="let item of items" [ngValue]="item.V">{{item.D}}</option>
        </select>
    </div>
</div>
<div>
  {{model}}
</div>
<div>
  <button (click)="load1()">Load list 1</button>
  <button (click)="load2()">Load list 2</button>
</div>

Activity

  1. DzmitryShylovich commented on Jan 12, 2017

    @DzmitryShylovich
    Contributor

    See #13268

  2. rongchaua commented on Jan 12, 2017

    @rongchaua
    Author

    @DzmitryShylovich: In my code I tried to bind ngModel with a native type (string. V-property has type of string) to avoid the object reference but it doesn't work either. Do you know if there is any workaround for this problem?

  3. DzmitryShylovich commented on Jan 12, 2017

    @DzmitryShylovich
    Contributor

    I tried to bind ngModel with a native type (string. V-property has type of string) to avoid the object reference but it doesn't work either

    pls add a small repro in plunkr

  4. rongchaua commented on Jan 12, 2017

    @rongchaua
    Author

    @DzmitryShylovich : I created a sample in plnkr.
    Steps:
    0. Firefox as Browser.

    1. Open plnkr.
    2. Select "Deutschland"
    3. Click on "Load list 2"

    Expected: Deutschland should be still selected.
    Real: Nothing is selected.

    http://embed.plnkr.co/YoSG91kx5YFS7SmV5qsM/

  5. DzmitryShylovich commented on Jan 12, 2017

    @DzmitryShylovich
    Contributor

    you need to use [value]="item.V" with primitive types
    [ngValue]="" for objects

  6. DzmitryShylovich commented on Feb 1, 2017

    @DzmitryShylovich
    Contributor
  7. pkozlowski-opensource commented on Feb 1, 2017

    @pkozlowski-opensource
    Member

    Fixed via f89d004, thnx @DzmitryShylovich

  8. angular-automatic-lock-bot commented on Sep 10, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions