当前位置: 首页 > 知识库问答 >
问题:

模型绑定无法使用angular2[重复]中的select

洪念
2023-03-14

我正在尝试设置下拉的默认值,我设置了一个默认值的模型,但它没有更新选择

请参阅此https://plnkr.co/edit/bnm5yxx78iykv2Opazol?p=preview

@Component({
    selector: 'my-app',
    template: `
    <h1>Hello</h1>
<select class="form-control selectpicker" (change)="changeTower()" [(ngModel)]="_selectedTower">
  <option attr.value="{{tower._id}}" *ngFor="#tower of _towers;">Tower {{tower.name}}</option>
</select>    
    `})
export class AppComponent {
  _towers = [{_id:1, name: '1'}, {_id:2, name: '2'}, {_id:3, name: '3'}];
  constructor() {
    this._selectedTower = 2 ;
  }

  changeTower() {
    setTimeout(()=>{
      alert( this._selectedTower);
    },100);
  }

共有1个答案

夏侯朝斑
2023-03-14
<select class="form-control selectpicker" #select (change)="changeTower(select.value)" [(ngModel)]="_selectedTower">
  <option value="{{tower._id}}" *ngFor="#tower of _towers;">Tower {{tower.name}}</option>
</select>  

changeTower(id) {

    //id is the selected value
}
 类似资料: