簡體   English   中英

如何將 ngModel 值與 angular 2 中的下拉選項列表綁定?

[英]How can i bind ngModel value with options list of dropdown in angular 2?

例子:

<p-dropdown name="senderId" [options]="senderIdList" [filter]="true" required 
    #senderIdDrpdwn="ngModel" [(ngModel)]="selectedSenderId"></p-dropdown>

在 senderIdList = 中包含所有值的列表。 在 selectedSenderId = 中具有來自 senderIdList 的值。

但是 output 僅在我 select 在下拉列表中顯示列表但我希望加載值是(在 ngModel 中)然后單擊下拉列表顯示所有列表(senderIdList)。

謝謝。

這是工作示例

  selected: any;
  cities = {};

  countries = [
    {
      id: 1,
      name: "Sri lanka"
    },
    {
      id: 2,
      name: "India"
    },

    {
      id: 3,
      name: "Japan"
    }
  ];

  ngOnInit() {

  }

  onChange(deviceValue) {
    console.log(
      "Changed Value is ",
      deviceValue,
      "name",
      this.countries[deviceValue].name
    );
    this.selected = this.countries[deviceValue - 1].name;
   }

.html

<select name="city" class="rounded-inputs20 select-select col-md-3" (change)="onChange($event.target.value)">
  <option *ngFor="let country of countries"  [value]="country.id" >{{country.name}}</option>
  </select>
{{selected}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM