prolink007 prolink007 - 3 months ago 32
TypeScript Question

select options not updating dynamically with angular 2 bindings

I am unable to get

select
options to update dynamically with
Angular2
bindings. How do you update the contents of the
select
dynamically?

Plnkr explaining my issue. Notice the
select
options never update to the new array i set.

Answer

Because when you are reassinging options value you have to point to options of current by using this before options.

handleChange(event) {
    console.log(event.target.value);
    //this to indicate options from current class
    this.options = [{id: 0, name:"First"}, {id: 1, name:"Second"}, {id: 2, name:"Third"}];

    this.selected1 = this.options.filter((option) => {
      return option.id == event.target.value;
    })[0];    
}

Demo Plunkr