How to assign selected value to “Select” when valu

2019-07-18 09:48发布

Using angular 2.2.4 from following links learned select list of class object:

How can I get new selection in "select" in Angular 2?

https://stackoverflow.com/a/35945293/4773561

Angular 2 Dropdown Options Default Value

& implemented as follows

<select id="st" name="state" [(ngModel)]="selectedState" (ngModelChange)="onStateSelectedForVanue($event)">
    <option *ngFor="let state of states" 
            [ngValue]="state">{{state.name}}
    </option>
</select>

here 'states' is list of State Class object

export class State {
 public id:number;
 public name:string;
}

this is list of class

public states : State[]= [];

for states getting value from Service,up to this code I get drop-down of 'state-name'.Now I want to show 'Default' of 'Pre-selected' State.For that created selectedState Class Object and assign through [(ngModel)] or [ngModel]

public selectedState: State;

in constructor follwing value assign:

this.selectedState = new State();
this.selectedState.id = 12;
this.selectedState.name = 'Goa';

and states list getting from ngOnInit() but default, no list item(state) is getting selected

also tried adding following code in

<option></option>
[selected]="state.name === selectedState.name" with reference link

Set initially selected item in Select list in Angular2

https://stackoverflow.com/a/37666951/4773561

https://stackoverflow.com/a/37663411/4773561

still not getting pre-selected state

what is wrong in code ?

0条回答
登录 后发表回答